简体   繁体   English

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

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

  • I have a simple gridview and sqldatasource1 to bind from tableData to gridview. 我有一个简单的gridview和sqldatasource1可以将tableData绑定到gridview。

A column called "Haha" type int . 名为“ Haha”的列类型为int。 the default value in the datatable for that cell is 0 . 该单元格数据表中的默认值为0。 On row update I want to increase the value by one. 在行更新时,我想将值增加一。

So right now in the cell the value is 0 . 因此,现在该单元格中的值为0。 If I just press the update button then the value in the data table will be 1 and if I update again it will be 2 and so on. 如果我只是按下更新按钮,那么数据表中的值将为1,如果我再次更新,其值为2,依此类推。

I've tried this code but is not working , I mean nothing is changed. 我已经试过了这段代码,但是没有用,我的意思是什么都没有改变。

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();
        } 
    }

Thanks 谢谢

try this 尝试这个

    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