简体   繁体   English

C# DataGridView 编辑单元格值

[英]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:我的表单上有 DataGridView 控件,它有 3 列 + 1 ButtonColumn,我向其中添加了如下几行:

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.我在 stackoverflow 上尝试了一些类似问题的答案,但效果不佳。 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:更新2:

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.该问题似乎与 DataGridViewButtonCell 相关,因为其他类型的单元格变化良好。

You need to change from using cell.Value to您需要从使用 cell.Value 更改为

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

Changing cells value only changes if you add that cell back to the datagridview.更改单元格值仅在您将该单元格添加回 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.设置后,将无法编辑 ButtonCell 的值。 Any readonly option of the DataGridView is irrelevant here, they specify if the user, not you, can edit cells. DataGridView 的任何只读选项在这里都无关紧要,它们指定用户(而不是您)是否可以编辑单元格。

Thanks for your help, @deathismyfriend感谢您的帮助,@deathismyfriend

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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