简体   繁体   中英

Windows Forms: Datagridview cell value not updated when it is modified

I am modifying the value in my Datagridview cell but it is not gettting updated in the cell. I have tried dataGridView.Refresh(), dataGridView.RefreshEdit() and dataGridView.EndEdit() but none of them work. What am I doing wrong?

 private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == dataGridView.Columns[browseDataGridViewButtonColumnName].Index)
    {
        var dialogResult = openFileDialog.ShowDialog();
        if (dialogResult != DialogResult.OK)
        {
            return;
        }

        var fileNameAndPath = openFileDialog.FileName;
        dataGridView[e.RowIndex, FileNameAndPathColumnIndex].Value = fileNameAndPath;

    }
}

Change your last line

dataGridView[e.RowIndex, FileNameAndPathColumnIndex].Value = fileNameAndPath;

To use Cells

dataGridView[e.RowIndex].Cells[e.ColumnIndex].Value = fileNameAndPath;

您可以使用以下解决方案来解决您的问题:

this.dataGridView1.RefreshEdit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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