簡體   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