Asp.net 单击按钮时使用文本框在网格视图中填充数据-它显示一个超出范围的错误
当我使用文本框插入数据时,它会显示索引越界错误Asp.net 单击按钮时使用文本框在网格视图中填充数据-它显示一个超出范围的错误,asp.net,Asp.net,当我使用文本框插入数据时,它会显示索引越界错误 Button_Click() DataTable dt = new DataTable(); DataRow dr; dt.Columns.Add("Name"); ///enter code here dt.Columns.Add("Address"); dt.Columns.Add("Number"); //First fill all the date present in the grid
Button_Click()
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("Name"); ///enter code here
dt.Columns.Add("Address");
dt.Columns.Add("Number");
//First fill all the date present in the grid
for (int intCnt = 0; intCnt < grd.Rows.Count; intCnt ++)
{
if (grd.Rows[intCnt].RowType == DataControlRowType.DataRow)
{
dr = dt.NewRow();
dr["Name"] = grd.Rows[intCnt].Cells[0].Value;/// at this point
dr["Address"] = grd.Rows[intCnt].Cells[1].Value;
dr["Number"] = grd.Rows[intCnt].Cells[2].Value;
dt.Rows.Add(dr);
}
}
dr = dt.NewRow();
dr["Name"] = txt1.Text;
dr["Address"] = txt2.Text;
dr["Number"] = txt3.Text;
dt.Rows.Add(dr);
grd.DataSource = dt;
grd.DataBind();
}
按钮\u单击()
DataTable dt=新的DataTable();
数据行dr;
dt.列。添加(“名称”)///在这里输入代码
dt.列。添加(“地址”);
dt.列。添加(“编号”);
//首先填写表格中的所有日期
对于(intCnt=0;intCnt
试试这个
必须在for循环中使用grd.Rows.Count-1
按钮点击()
DataTable dt=newdatatable();
数据行dr;
dt.列。添加(“名称”);
dt.列。添加(“地址”);
dt.列。添加(“编号”);
对于(intCnt=0;intCnt
希望这能帮助您将for循环的条件更改为for(int-int-cnt=0;int-cnt
而不是(intCnt=0;intCnt
{...}
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("Name");
dt.Columns.Add("Address");
dt.Columns.Add("Number");
for (int intCnt = 0; intCnt < grd.Rows.Count-1; intCnt ++)
{
if (grd.Rows[intCnt].RowType == DataControlRowType.DataRow)
{
dr = dt.NewRow();
dr["Name"] = grd.Rows[intCnt].Cells[0].Value;/// at this point
dr["Address"] = grd.Rows[intCnt].Cells[1].Value;
dr["Number"] = grd.Rows[intCnt].Cells[2].Value;
dt.Rows.Add(dr);
}
}
dr = dt.NewRow();
dr["Name"] = txt1.Text;
dr["Address"] = txt2.Text;
dr["Number"] = txt3.Text;
dt.Rows.Add(dr);
grd.DataSource = dt;
grd.DataBind();
}