【问题标题】:GridView : how to increase a value from a cell by one on row updating?GridView:如何在行更新时将单元格中的值增加一个?
【发布时间】:2012-04-03 21:46:35
【问题描述】:
  • 我有一个简单的 gridview 和 sqldatasource1 从 tableData 绑定到 网格视图。

名为“哈哈”的列类型 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();
        } 
    }

谢谢

【问题讨论】:

  • 这个问题和SQL有什么关系?您是从数据库中获取数据还是将这些数据(更新后的数据)存储到表中......?
  • 我有一个数据库和一个表,我正在使用 sqldatasource 将表中的信息绑定到 grdiview。
  • 这就是我所要求和期待的 .. 所以一件事 .. 你想在点击那个按钮的同时更新你的数据库吗.. ??

标签: c# asp.net sql


【解决方案1】:

试试这个

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    • 2019-10-04
    • 2013-06-20
    • 1970-01-01
    相关资源
    最近更新 更多