I'm adding dynamic Rows in ASP.Net GridView Control with TextBoxes. But I'm getting the error below when I click on my Add New Row button.

Specified argument was out of the range of valid values. Parameter name: index

What could be the error ?

Th code in my .aspx file

<div> <asp:gridview runat="server" autogeneratecolumns="false" gridlines="None" width="100%" height="100%"> <Columns> <asp:TemplateField> <ItemTemplate> <div> <div> <div> <asp:Label runat="server" Text="Type Name" CssClass="formLable"></asp:Label> </div> <div> <asp:TextBox Width="200" runat="server" CssClass="txtbox"></asp:TextBox> </div> <span> <asp:RequiredFieldValidator runat="server" ControlToValidate="txt_type" ErrorMessage="Type is required." Display="Dynamic" CssClass="error"></asp:RequiredFieldValidator> </span> </div> <div> <div> <asp:Label runat="server" Text="Total" CssClass="formLable"></asp:Label> </div> <div> <asp:TextBox Width="200" runat="server" CssClass="txtbox"></asp:TextBox> </div> </div> </div> <div> <div> <div> <asp:Label runat="server" Text="Max" CssClass="formLable"></asp:Label> </div> <div> <asp:TextBox Width="200" runat="server" CssClass="txtbox"></asp:TextBox> </div> </div> <div> <div> <asp:Label runat="server" Text="Min" CssClass="formLable"></asp:Label> </div> <div> <asp:TextBox Width="200" runat="server" CssClass="txtbox"></asp:TextBox> </div> </div> <div> <div> <asp:Label runat="server" Text="Rate" CssClass="formLable"></asp:Label> </div> <div> <asp:TextBox Width="200" runat="server" CssClass="txtbox"></asp:TextBox> </div> </div> </div> <asp:Button runat="server" Text="Add New Row" OnClick="ButtonAdd_Click" CausesValidation="False" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:gridview> </div> 

C# code of ButtonAdd_Click()

if (ViewState["CurrentTable"] != null) { DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"]; DataRow drCurrentRow = null; if (dtCurrentTable.Rows.Count > 0) { for (int i = 1; i <= dtCurrentTable.Rows.Count; i++) { //extract the TextBox values TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("txt_type"); TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("txt_total"); TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("txt_max"); TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("txt_min"); TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("txt_rate"); drCurrentRow = dtCurrentTable.NewRow(); dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text; dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text; dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text; dtCurrentTable.Rows[i - 1]["Column4"] = box4.Text; dtCurrentTable.Rows[i - 1]["Column5"] = box5.Text; rowIndex++; } dtCurrentTable.Rows.Add(drCurrentRow); ViewState["CurrentTable"] = dtCurrentTable; Gridview1.DataSource = dtCurrentTable; Gridview1.DataBind(); } } else { Response.Write("ViewState is null"); } 
8

3 Answers

It seems that you are trying to get 5 items out of a collection with 5 items. Looking at your code, it seems you're starting at the second value in your collection at position 1. Collections are zero-based, so you should start with the item at index 0. Try this:

TextBox box1 = (TextBox)Gridview1.Rows[i].Cells[0].FindControl("txt_type"); TextBox box2 = (TextBox)Gridview1.Rows[i].Cells[1].FindControl("txt_total"); TextBox box3 = (TextBox)Gridview1.Rows[i].Cells[2].FindControl("txt_max"); TextBox box4 = (TextBox)Gridview1.Rows[i].Cells[3].FindControl("txt_min"); TextBox box5 = (TextBox)Gridview1.Rows[i].Cells[4].FindControl("txt_rate"); 
2

try this.

if (ViewState["CurrentTable"] != null) { DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"]; DataRow drCurrentRow = null; if (dtCurrentTable.Rows.Count > 0) { for (int i = 1; i <= dtCurrentTable.Rows.Count; i++) { //extract the TextBox values TextBox box1 = (TextBox)Gridview1.Rows[i].Cells[1].FindControl("txt_type"); TextBox box2 = (TextBox)Gridview1.Rows[i].Cells[2].FindControl("txt_total"); TextBox box3 = (TextBox)Gridview1.Rows[i].Cells[3].FindControl("txt_max"); TextBox box4 = (TextBox)Gridview1.Rows[i].Cells[4].FindControl("txt_min"); TextBox box5 = (TextBox)Gridview1.Rows[i].Cells[5].FindControl("txt_rate"); drCurrentRow = dtCurrentTable.NewRow(); drCurrentRow["RowNumber"] = i + 1; dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text; dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text; dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text; dtCurrentTable.Rows[i - 1]["Column4"] = box4.Text; dtCurrentTable.Rows[i - 1]["Column5"] = box5.Text; rowIndex++; } dtCurrentTable.Rows.Add(drCurrentRow); ViewState["CurrentTable"] = dtCurrentTable; Gridview1.DataSource = dtCurrentTable; Gridview1.DataBind(); } } else { Response.Write("ViewState is null"); } 
1

I was also getting same issue as i tried using value 0 in non-based indexing,i.e starting with 1, not with zero

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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