【发布时间】:2014-10-26 23:44:32
【问题描述】:
全部。我已经在谷歌上搜索了一个小时这个问题,但仍然无法理解它是如何工作的。我的表单上有 DataGridView 控件,它有 3 列 + 1 个 ButtonColumn,我在其中添加如下行:
dg.Rows.Add(param1, param2, param3);
按钮的文本设置如下:
DataGridViewButtonColumn bc = (DataGridViewButtonColumn)dg.Columns["ButtonColumn"];
bc.Text = "Action";
bc.UseColumnTextForButtonValue = true;
现在,我想更改特定按钮的文本,一旦用户单击它,就可以说“完成”。我试过这样:
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();
}
}
而且它不起作用。我在stackoverflow上尝试了一些类似问题的答案,但效果不佳。如果我忽略了什么,请原谅我。有人会这么好心地解释我如何做到这一点,为什么这不起作用?
更新:
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";
}
}
更新 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();
}
}
我什至无法在调试器中手动更改值,它会立即设置回旧值。 这个问题似乎是 DataGridViewButtonCell 的问题,因为其他类型的单元格变化很好。
【问题讨论】:
标签: c# winforms datagridview