在datarow中编辑绑定

时间:2017-03-25 08:54:42

标签: asp.net gridview edit datarow

我有很多col的gridview将使用rowdatabound总和并显示总col并且它仅在视图中工作,但是当我尝试使用editview编辑时出现gridview出现此错误的问题(故障排除异常:系统.nu​​llreferenceexception)

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string val1 = e.Row.Cells[13].Text; //Gets the value in Column 1
        string val2 = e.Row.Cells[14].Text; //Gets the value in Column 2
        string val3 = e.Row.Cells[15].Text; //Gets the value in Column 3
        string val4 = e.Row.Cells[16].Text; //Gets the value in Column 4
        Label lblTotal = (Label)e.Row.Cells[12].FindControl("Label1"); //
        float _val1, _val2, _val3, _val4;

        float.TryParse(val1, out _val1);
        float.TryParse(val2, out _val2);
        float.TryParse(val3, out _val3);
        float.TryParse(val4, out _val4);

        float sum = _val1 + _val2 + _val3 + _val4;
        lblTotal.Text += sum.ToString();
    }
}

1 个答案:

答案 0 :(得分:0)

您需要检查DataRow是否处于编辑模式。

if (e.Row.RowType == DataControlRowType.DataRow)
{
    if ((e.Row.RowState & DataControlRowState.Edit) > 0)
    {
        //row is in edit mode
    }
    else
    {
        //a normal row
    }
}