“指定的参数超出了有效值的范围”
我正在使用TextBoxes在ASP.Net GridView控件中添加动态行。 但是当我点击我的添加新行按钮时,我收到以下错误。
指定的参数超出了有效值的范围。 参数名称:index
可能是什么错误?
我的.aspx文件中的代码
ButtonAdd_Click()的C#代码
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"); }
您似乎想要从5个项目的集合中获取5个项目。 看看你的代码,你似乎从第1位的集合中的第二个值开始。集合是从零开始的,所以你应该从索引0处的项开始。试试这个:
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");
试试这个。
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"); }
我也遇到了同样的问题,因为我尝试在非基础索引中使用值0,即从1开始,而不是从0开始
上述就是C#学习教程:“指定的参数超出了有效值的范围”分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注---计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/955070.html