Hello
I have the next code (which is creating a dynamically GridView from TextBox values)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; using System.Data.SqlTypes; using System.Data.Sql; using System.Web.Configuration; using System.Text; public partial class test1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } private void BindGrid(int rowcount) { //int MyIdentity = e.RowIndex; DataTable dt = new DataTable(); DataRow dr; dt.Columns.Add(new System.Data.DataColumn("TextBox1Column", typeof(String))); dt.Columns.Add(new System.Data.DataColumn("TextBox2Column", typeof(String))); dt.Columns.Add(new System.Data.DataColumn("TextBox3Column", typeof(String))); if (ViewState["CurrentData"] != null) { for (int i = 0; i < rowcount + 1; i++) { dt = (DataTable)ViewState["CurrentData"]; if (dt.Rows.Count > 0) { dr = dt.NewRow(); dr[0] = dt.Rows[0][0].ToString(); } } dr = dt.NewRow(); dr[0] = TextBox1.Text; dr[1] = TextBox2.Text; dr[2] = TextBox3.Text; dt.Rows.Add(dr); } else { dr = dt.NewRow(); dr[0] = TextBox1.Text; dr[1] = TextBox2.Text; dr[2] = TextBox3.Text; dt.Rows.Add(dr); } // If ViewState has a data then use the value as the DataSource if (ViewState["CurrentData"] != null) { GridView1.DataSource = (DataTable)ViewState["CurrentData"]; GridView1.DataBind(); } else { // Bind GridView with the initial data assocaited in the DataTable GridView1.DataSource = dt; GridView1.DataBind(); } // Store the DataTable in ViewState to retain the values ViewState["CurrentData"] = dt; } protected void Button1_Click(object sender, EventArgs e) { { // Check if the ViewState has a data assoiciated within it. If if (ViewState["CurrentData"] != null) { DataTable dt = (DataTable)ViewState["CurrentData"]; int count = dt.Rows.Count; BindGrid(count); } else { BindGrid(1); } TextBox1.Text = string.Empty; TextBox2.Text = string.Empty; TextBox3.Text = string.Empty; TextBox1.Focus(); } } protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { GridView1.DeleteRow(GridView1.SelectedIndex); } }
(the code is from http://geekswithblogs.net/dotNETvinz/archive/2008/05/07/adding-multiple-columns-and-rows-in-gridview-without-using-a-again.aspx)
And i tried to add the
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { GridView1.DeleteRow(GridView1.SelectedIndex); }
But i have the next error
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
The code from aspx page is
<asp:TextBox ID="TextBox1" runat="server"/><asp:TextBox ID="TextBox2" runat="server"/><asp:TextBox ID="TextBox3" runat="server"/><asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /><asp:GridView ID="GridView1" runat="server" OnRowDeleting="GridView1_RowDeleting" AutoGenerateDeleteButton="True"></asp:GridView>
and AutoGenerateDeleteButton is set to True.
Can you help me please to change the code so i can delete rows from the GridView
Thank you