0

This is my code iam trying to display the data in gridview which is entered in the input text box fields after hitting the submit button.

There is no error but the data is not add to the table it just shows the empty table

public partial class Default : System.Web.UI.Page
{
    DataRow dr;
    DataTable dt = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
           DataBind();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        dt.Columns.Add(new DataColumn("ID", typeof(int)));
        dt.Columns.Add(new DataColumn("Name", typeof(string)));
        dt.Columns.Add(new DataColumn("Salary", typeof(int)));
        dt.Columns.Add(new DataColumn("Department", typeof(string)));

        dr = dt.NewRow();
        dr["ID"] = txtID.Text;
        dr["Name"] = txtName.Text;
        dr["Salary"] = txtSalary.Text;
        dr["Department"] = txtDepartment.Text;
        dt.Rows.Add(dr);

        GridView1.DataSource = dt;
        GridView1.DataBind();
3
  • Please help me with this Commented May 24, 2016 at 22:24
  • 1
    Make sure you have selected AutoGenerateColumns property to true Commented May 24, 2016 at 22:40
  • can you brief about databind in pageload event Commented May 25, 2016 at 5:14

1 Answer 1

1

Remember web is stateless which means that every time you want to add a row to the gridview you need to store the current data somewhere.The example below uses ViewState.There are of course many ways to store state besides ViewState such as Session,localStorage in the browser and database storage,just to name a few.

Code behind:

  protected void Page_Load(object sender, EventArgs e)
    {
    }

    private void DataBind()
    {
        DataTable table = ViewState["Data"] as DataTable;
        GridView1.DataSource = table;
        GridView1.DataBind();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        DataRow dr = null;
        DataTable dt = ViewState["Data"] as DataTable;

        if (dt == null)
        {
            dt = new DataTable();
            dt.Columns.Add(new DataColumn("ID", typeof(int)));
            dt.Columns.Add(new DataColumn("Name", typeof(string)));
            dt.Columns.Add(new DataColumn("Salary", typeof(int)));
            dt.Columns.Add(new DataColumn("Department", typeof(string)));
        }

        dr = dt.NewRow();
        dr["ID"] = txtID.Text;
        dr["Name"] = txtName.Text;
        dr["Salary"] = txtSalary.Text;
        dr["Department"] = txtDepartment.Text;

        dt.Rows.Add(dr);

        ViewState["Data"] = dt;

        DataBind();
    }

.ASPX:

<body>
    <form id="form1" runat="server">
        <table>
            <tr>
                <td>ID</td>
                <td><asp:TextBox ID="txtID" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Name</td>
                <td><asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Salary</td>
                <td><asp:TextBox ID="txtSalary" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Department</td>
                <td><asp:TextBox ID="txtDepartment" runat="server"></asp:TextBox></td>
                <td><asp:Button ID="Button1" runat="server" Text="Add to grid" OnClick="Button1_Click" /></td>
            </tr>
        </table>
        <asp:GridView ID="GridView1" runat="server"></asp:GridView>
    </form>
</body>

Output: Adding data to gridview

Sign up to request clarification or add additional context in comments.

5 Comments

Worked. thanq you so much Denis Wassel.
Denis, If i wan the Txt boxes empty after i hit Add to grid??
Something like txtName.Text = String.Empty; but use google,you can find all those little things online
If i want to hard code some values to that table what do i do without using the data base or importing any data from the database. Please help me out. Thanks in advance.
Hi Denis, Can you help me with this for the code you gave as above i want to hard code come values to that Data Table. What do i do for that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.