【问题标题】:C# DataGridView edit cell valueC# DataGridView 编辑单元格值
【发布时间】: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


    【解决方案1】:

    你需要从使用 cell.Value 改为

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

    仅当您将该单元格添加回 datagridview 时,更改单元格的值才会发生变化。 这样你就可以摆脱像这样使用单元格了。

    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";
        }
    }
    

    我删除了其余部分,因为我认为您正在做的事情不需要它。

    【讨论】:

    • @Stranger1399 会发生什么?不起作用不是一个好的解释。
    • 很公平。该值保持不变,当我在调试模式下单步执行代码时,它不会改变。
    • @Stranger1399 试试上面的代码。它应该可以工作,我刚刚在我的 DataGridView 上对其进行了测试。我使用单元格单击事件而不是数据网格视图的单元格内容单击事件。我不确定单元格内容点击事件是否适用于上述情况。
    • 还是什么都没有。单元格本身没有 EditMode 属性。我不得不将它更改为articles.EditMode = DataGridViewEditMode.EditProgrammatically,否则它不会编译。也许这就是我的问题所在? Windows XP、VisualStudio 2010、.NET 4.0
    • @Stranger1399 抱歉,我不小心将其更改为单元格。编辑模式应该适用于您的数据网格视图。检查以确保编辑模式正在更改。不过它应该可以工作。
    【解决方案2】:

    问题出在这行代码中:

    bc.UseColumnTextForButtonValue = true;
    

    设置后,无法编辑 ButtonCell 的值。 DataGridView 的任何只读选项在这里都无关紧要,它们指定用户(而不是您)是否可以编辑单元格。

    感谢您的帮助,@deathismyfriend

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-25
      • 2011-12-24
      • 1970-01-01
      • 2012-04-05
      • 2015-05-28
      • 2015-01-11
      相关资源
      最近更新 更多