繁体   English   中英

GridView:如何在单元格上逐行更新增加一个值?

[英]GridView : how to increase a value from a cell by one on row updating?

  • 我有一个简单的gridview和sqldatasource1可以将tableData绑定到gridview。

名为“ Haha”的列类型为int。 该单元格数据表中的默认值为0。 在行更新时,我想将值增加一。

因此,现在该单元格中的值为0。 如果我只是按下更新按钮,那么数据表中的值将为1,如果我再次更新,其值为2,依此类推。

我已经试过了这段代码,但是没有用,我的意思是什么都没有改变。

GridViewRow currentRow = ((GridView)sender).Rows[e.RowIndex];
        String intColumnText = currentRow.Cells[2].Text; //assuming it's the first cell
        int value;
        if (int.TryParse(intColumnText, out value))
        {
            //if you want to increment that value and override the old
            currentRow.Cells[2].Text = (value++).ToString();
        } 
    }

谢谢

尝试这个

    GridViewRow currentRow = ((GridView)sender).Rows[e.RowIndex];
    String intColumnText = currentRow.Cells[2].Text; //assuming it's the first cell
    int value;
    if (int.TryParse(intColumnText, out value))
    {
        //if you want to increment that value and override the old

((DataTable)((GridView)sender).DataSource).Rows[CurrentRow.RowIndex][2]=(value +1).ToString();
    } 
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM