Asp Fill Gridview with textbox

0

I am new to this, I would like to know how I can fill this gridview that I have with the following textbox, I will leave the code and the images as I do not know how to mount them here.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %>

              

    <asp:Label ID="Label1" runat="server" Text="Nombre"></asp:Label>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <br />

    <asp:Label ID="Label2" runat="server" Text="Apellido"></asp:Label>
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    <br />

    <asp:Label ID="Label3" runat="server" Text="telefono"></asp:Label>
    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
    <br />
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
        Text="poner" />

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
        <Columns>
            <asp:BoundField DataField="nombre" HeaderText="nombre" />
            <asp:BoundField DataField="apellido" HeaderText="apellido" />
        </Columns>
    </asp:GridView>

</div>
</form>

public partial class _Default : System.Web.UI.Page
{


    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        //String[] nombre;
        //String[] apellido;

        //DataTable dt = new DataTable();           
        //DataRow Row1;            

        //dt.Columns.Add(new DataColumn("Nombre", System.Type.GetType("System.String")));
        //dt.Columns.Add(new DataColumn("Apellido", System.Type.GetType("System.String")));

        //Row1 = dt.NewRow();
        //nombre = new string[] {TextBox1.Text };
        //apellido = new string[] { TextBox2.Text };

        //dt.Rows.Add(Row1);
        //dt.AcceptChanges();


        //GridView1.DataSource = dt;
        //GridView1.DataBind();
        //--------------------------------------------------------------------------------------------------
        //DataSet ds = new DataSet();
        //DataTable dt = new DataTable();
        //ds.Tables.Add(dt);
        //dt.Columns.Add("Nombre", typeof(string));
        //dt.Columns.Add("Apellido", typeof(string));
        //dt.Rows.Add(TextBox1.Text, TextBox2.Text);
        //GridView1.DataSource = dt;
        //GridView1.DataBind();
        //--------------------------------------------------------------------------------------------------
        DataSet ds = new DataSet();

        ds.Tables.Add("0");
        ds.Tables["0"].Columns.Add("Nombre", Type.GetType("System.String"));
        ds.Tables["0"].Columns.Add("Apellido", Type.GetType("System.String"));


        DataRow row = ds.Tables["0"].NewRow();

        row["Nombre"] = "Rafael";
        row["Apellido"] = "Méndez";

        ds.Tables["0"].Rows.Add(row);

        ds.Tables["0"].AcceptChanges();
        GridView1.DataSource = ds.Tables["0"];
        GridView1.DataBind();
    }


}
    
asked by psy 28.05.2018 в 16:15
source

1 answer

0

I do not understand well what you want to achieve, but assuming that you want to take the values that the user enters, they are shown in the grid and I do it in the following way:

 protected void Button1_Click(object sender, EventArgs e)
{
    try
    {
        cn = new OdbcConnection(CadenaConexion);
        using (cmd = new OdbcCommand(MyString, cn))
        {
            cn.Open();
            using (cmd = new OdbcCommand("INSERT INTO TABLA(Columna1, Columna2) values ('"+TextBox1+"', '"+TextBox2+"'), cn))
            {

                OdbcDataReader d = cmd.ExecuteReader();
                if (d.Read())
                {
                    lblMensaje.Text = ("Error al actualizar datos.");
                }
                else
                {
                    lblMensaje.Text = ("Datos actualizados.");
                }
                Grilla.DataBind(); //Grilla es el nombre del GridView
            }
        }

    }
    catch (Exception ex)
    {
        Response.Write(ex.ToString());



    }


}
    
answered by 28.05.2018 в 18:12