简体   繁体   中英

C# DataGridView edit cell value

All. I've been googling this question for an hour now and still can't understand how it works. I have DataGridView control on my form, which has 3 columns + 1 ButtonColumn, to which I add lines like this:

dg.Rows.Add(param1, param2, param3);

The text for the button is set like this:

DataGridViewButtonColumn bc = (DataGridViewButtonColumn)dg.Columns["ButtonColumn"];
bc.Text = "Action";
bc.UseColumnTextForButtonValue = true;

Now, I want to change specific button's text, once user clicked on it, to, let's say "Done". I tried it like this:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) {
    if (articles.Rows[e.RowIndex].Cells[e.ColumnIndex].GetType() == typeof(DataGridViewButtonCell)) {
        DataGridViewButtonCell cell = (DataGridViewButtonCell)articles.Rows[e.RowIndex].Cells[e.ColumnIndex];
            articles.CurrentCell = cell;
            articles.EditMode = DataGridViewEditMode.EditProgrammatically;
            articles.BeginEdit(false);
            cell.Value = "Done";
            articles.EndEdit();
    }
}

And it doesn't work. I've tried a few answers to a similar questions here, on stackoverflow, but it doesn't work as well. Forgive me if I overlooked something. Would anyone be so kind to explain me how to do this, and why doesn't this work?

UPDATE:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) {
    if (articles.Rows[e.RowIndex].Cells[e.ColumnIndex].GetType() == typeof(DataGridViewButtonCell)) {
         articles.EditMode = DataGridViewEditMode.EditProgrammatically;
         articles.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "Done";
    }
}

UPDATE2:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) {
if (articles.Rows[e.RowIndex].Cells[e.ColumnIndex].GetType() == typeof(DataGridViewButtonCell)) {

articles.EditMode = DataGridViewEditMode.EditProgrammatically;
articles.ReadOnly = false;
articles.Rows[e.RowIndex].ReadOnly = false;
articles.Rows[e.RowIndex].Cells[e.ColumnIndex].ReadOnly = false;
articles.CurrentCell = articles.Rows[e.RowIndex].Cells[e.ColumnIndex];
articles.BeginEdit(true);
if (articles.Rows[e.RowIndex].Cells[e.ColumnIndex].IsInEditMode) { //it's false here
    articles.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "Done";
}
articles.EndEdit();
}
}

I can't even change the Value manually in debugger, it's immediately set back to the old value. The problem seems to be sepcific to the DataGridViewButtonCell, because cells of other types change fine.

You need to change from using cell.Value to

articles.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "Done";

Changing cells value only changes if you add that cell back to the datagridview. This way you can get rid of using the cell like this.

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) {
    if (articles.Columns[e.ColumnIndex].GetType() == typeof(DataGridViewButtonColumn)) {
            articles.EditMode = DataGridViewEditMode.EditProgrammatically;
            articles.Rows[e.RowIndex].Cells[e.ColumnIndex].ReadOnly = false;
            articles.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "Done";
    }
}

I edited out the rest as I don't believe it is needed for what you are doing.

The problem was in this line of code:

bc.UseColumnTextForButtonValue = true;

When it's set, it's not possible to edit ButtonCell's value. Any readonly option of the DataGridView is irrelevant here, they specify if the user, not you, can edit cells.

Thanks for your help, @deathismyfriend

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