Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to display a data in gridview what i have enter in textboxes by using datatable

i have add the following in source part
XML
<form id="form1" runat="server">
  <div>

      <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
      <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
      <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
      <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
          style="height: 26px" Text="Button" />
      <br />
      <br />
      <br />
      <asp:GridView ID="GridView1" runat="server">
      </asp:GridView>
      <br />

  </div>
  </form>

in button click event

i have write the following code
C#
{
        DataTable dt = new DataTable(); 
        DataRow dr; 
        dt.Columns.Add("Name");
        dt.Columns.Add("Address"); 
        dt.Columns.Add("Number"); 
        //First fill all the date present in the grid 
        for(int intCnt=0;intCnt<gridview1.rows.count;intcnt++)>
                { 
            if (GridView1.Rows[intCnt].RowType == DataControlRowType.DataRow)
            {
                dr = dt.NewRow();
                dr["Name"] =GridView1.Rows[intCnt].Cells[0].Value; 
                dr["Address"] = GridView1.Rows[intCnt].Cells[1].Value; 
                dr["Number"] = GridView1.Rows[intCnt].Cells[2].Value;
                dt.Rows.Add(dr);
            }
        }
        dr = dt.NewRow();
        dr["Name"] = TextBox1.Text;
        dr["Address"] = TextBox2.Text;
        dr["Number"] = TextBox3.Text;
        dt.Rows.Add(dr); 
       Gridview1.DataSource = dt;
        Gridview1.DataBind();
    }

when i run the asp.net application

the following compile error will occur
Compiler Error Message: CS1061: 'System.Web.UI.WebControls.TableCell' does not contain a definition for 'Value' and no extension method 'Value' accepting a first argument of type 'System.Web.UI.WebControls.TableCell' could be found (are you missing a using directive or an assembly reference?)
Posted
Updated 2-Jan-12 23:12pm
v2

1 solution

Try this,

protected void Button1_Click(object sender, EventArgs e)
    {
        DataTable dt = null;
        try
        {
            if (Session["dtItems"] != null)
            {
                dt = (DataTable)Session["dtItems"];
            }
            else
            {
                dt = new DataTable();
                dt.Columns.Add("Quantity");
                dt.Columns.Add("Rate_Unit");
                dt.Columns.Add("Amount");
            }
            DataRow dataRow;
            dataRow = dt.NewRow();
            dataRow["Quantity"] = TextBox1.Text;
            dataRow["Rate_Unit"] = TextBox2.Text;
            dataRow["Amount"] = TextBox3.Text;
            dt.Rows.Add(dataRow);
            dt.AcceptChanges();
            Session["dtItems"] = dt;
            GridView1.DataSource = dt.DefaultView;
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
        }
    }
 
Share this answer
 
Comments
Reshma89 3-Jan-12 5:30am    
Thank u so much supriya.... it's done
Reshma89 3-Jan-12 5:47am    
Hi supriya
it can done
now my question is data is displayed in gridview what i have entered in textboxes
so i want to update ,delete with their data in gridview how it can be possible
plz suggest
Supriya Srivastav 3-Jan-12 7:28am    
Welcome dear,and if it is solved then mark it as solved.......
and for updation,deletion in gridview, just post a new ques. regarding that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900