Gridview rowupdate

时间:2017-07-19 19:29:51

标签: c# asp.net

我正在使用gridview_rowupdating。对于表中的6列,两列包含空值,当单击编辑按钮时,用户将在Rate列中输入一个整数。单击更新后,必须将值返回到gridview 。我在rowupdating事件中遇到问题。我收到以下提到的错误消息。

 //Retrieve the table from the session object.
    DataTable dt = (DataTable)Session["datasource"];

    //Update the values.           
    GridViewRow row = GridView2.Rows[e.RowIndex];

    dt.Rows[row.DataItemIndex]["Rate"] = ((TextBox)(row.Cells[5].Controls[0])).Text;
           //Reset the edit index.
    GridView2.EditIndex = -1;
    //Bind data to the GridView control.
    BindData();

    Session["datasource"] = dt;

错误: :'指定的参数超出了有效值的范围。' 但这个错误没有任何意义。我正在为列速率输入整数。

2 个答案:

答案 0 :(得分:0)

gridview行上的单元格集合为零索引,因此gridview“Rate”中的第5列实际上是索引4.尝试:

//Retrieve the table from the session object.
DataTable dt = (DataTable)Session["datasource"];

//Update the values.      
DataRow dataRow = dt.Rows[row.DataItemIndex];    
dataRow["Rate"] = Int32.Parse(e.NewValues["Rate"].ToString());

GridView2.EditIndex = -1;
//Bind data to the GridView control.
BindData();

Session["datasource"] = dt;

答案 1 :(得分:-1)

当Rate的类型为int和

((TextBox)(row.Cells[5].Controls[0])).Text 

是字符串,它无法正常工作。尝试将文本解析为int。