Click here to Skip to main content
15,900,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In the following code I am able to insert one row into a GView on click of 'begin' button. I would like to know how to insert new rows with the click of 'add new' button.
Can any one guide me ?

XML
<asp:GridView ID="GridView1" runat="server" Font-Names="Arial" Font-Size="XX-Small"
Style="z-index: 118; left: 174px; position: absolute; top: 421px" Width="543px" Height="66px" EmptyDataText="--- Empty Grid ---" AllowPaging="True" Caption="Customer Status Report" CaptionAlign="Top" DataKeyNames="Item #" >
</asp:GridView>
<asp:textbox ID="Textbox1" runat="server"></asp:textbox>
<asp:textbox ID="Textbox2" runat="server"></asp:textbox>
<asp:Button ID="Button2" runat="server" Text="Begin" OnClick="Button2_Click" />
<asp:Button ID="Button3" runat="server" Text="Add new" OnClick="Button3_Click"
style="z-index: 1;font-family:Sans-Serif; height:22px; left:122px;  margin-top:100px; font-weight:900; "  />


C#
protected void Button2_Click(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    dt.Columns.Add(new DataColumn("Item #", typeof(int)));
    dt.Columns.Add(new DataColumn("Customer Name", typeof(string)));
    dt.Columns.Add(new DataColumn("Contract Number", typeof(string)));
    DataRow dr = dt.NewRow();
    dr["Item #"] = 1;// i;
    dr["Customer Name"] = Textbox1.Text;
    dr["Contract Number"] = Textbox2.Text;
    dt.Rows.Add(dr);
    this.GridView1.Visible = true;
    GridView1.DataSource = dt;
    GridView1.DataBind();
}
protected void Button3_Click(object sender, EventArgs e) //to add new rows to GV
{
}
Posted
Updated 22-Oct-13 23:36pm
v2

Ithink my friend this shuold help you


protected void Button2_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
// in dt fill first all record which you retriv or shows/Bind in gridview
than add this code//

dt.Columns.Add(new DataColumn("Item #", typeof(int)));
dt.Columns.Add(new DataColumn("Customer Name", typeof(string)));
dt.Columns.Add(new DataColumn("Contract Number", typeof(string)));
dr = dt.NewRow();
dr["Item #"] = i;
dr["Customer Name"] = "";
dr["Contract Number"] ="";
dt.Rows.Add(dr);


this.GridView1.Visible = true;
GridView1.DataSource = dt;
GridView1.DataBind();
}
 
Share this answer
 

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