【问题标题】:Edit entire row in a DataGridView (not only one cell)在 DataGridView 中编辑整行(不仅是一个单元格)
【发布时间】:2013-08-20 21:14:29
【问题描述】:

有人可以建议如何做到这一点吗?

目前我有:

  1. 具有四列的DataGridView - Text1 |文本2 |编辑按钮 |保存按钮
  2. 当我点击 Text1 时,它变成一个可编辑的字段,我可以 改变它的价值
  3. 然后我尝试编辑 Text2,但单击此单元格会将我的更改保存在 Text1 中

问题是:当我尝试编辑第二个字段(Text2)时,第一个(Text1)失去焦点,退出编辑模式并保存我所做的更改,同时我想保存所有更改同时排成一排。

我想实现什么:

  1. 我按下 EditButton,所有单元格都变为可编辑状态,因此我可以连续更改任何单元格的值
  2. 我改变 Text1 的值,然后改变 Text2 的值
  3. 只有当我按下 SaveButton 时,它才会保存所做的更改

问题是:在我按下特定按钮之前,如何让所有单元格保持连续处于编辑模式?

【问题讨论】:

标签: c# winforms datagridview


【解决方案1】:

也许你可以像这样使用自定义 DataGridView

public class CustomDGV : DataGridView
{
    private object _cellValue;
    private Dictionary<int, object[]> _pendingChanges;

    public CustomDGV()
    {
        _pendingChanges = new Dictionary<int, object[]>();
    }

    protected override void OnCellBeginEdit(DataGridViewCellCancelEventArgs e)
    {
        // Save the value of the cell before edit
        _cellValue = this[e.ColumnIndex, e.RowIndex].Value;

        // If there's already a pending change for that cell, display the edited value
        if (_pendingChanges.ContainsKey(e.RowIndex))
        {
            this[e.ColumnIndex, e.RowIndex].Value = _pendingChanges[e.RowIndex][e.ColumnIndex];
        }

        base.OnCellBeginEdit(e);
    }

    protected override void OnCellEndEdit(DataGridViewCellEventArgs e)
    {
        // Adds the edited value of the cell into a dictionary
        if (!_pendingChanges.ContainsKey(e.RowIndex))
        {
            _pendingChanges.Add(e.RowIndex, new object[this.ColumnCount]);
        }

        _pendingChanges[e.RowIndex][e.ColumnIndex] = this[e.ColumnIndex, e.RowIndex].Value;

        // Display the "old" value
        this[e.ColumnIndex, e.RowIndex].Value = _cellValue;
    }

    public void SavePendingChanges(int rowIndex)
    {
        if (_pendingChanges.ContainsKey(rowIndex))
        {
            // Gets the pending changes for that row
            var rowData = _pendingChanges[rowIndex];
            // Update every cell that's been edited
            for(int i = 0; i < rowData.Length; i++)
            {
                if (rowData[i] != null)
                    this[i, rowIndex].Value = rowData[i];
            }
            // Removes the pending changes from the dictionary once it's saved
            _pendingChanges.Remove(rowIndex);
        }
    }
}

在 CellContentClick 上,您可以调用 SavePendingChanges()

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex > -1 && e.RowIndex > -1)
    {
        if (e.ColumnIndex == 3) // Save button
        {
            dataGridView1.SavePendingChanges(e.RowIndex);
        }
    }
}

【讨论】:

  • +1 表示建议,虽然这不是我的情况,因为我每秒都在更新网格中的值,所以在字典中保存更改对我来说还不够,因为我还需要在网格时隐藏更新值在编辑模式下(对于用户来说,即使他已经编辑了它,看到该值仍在变化会感到困惑)
【解决方案2】:

好的,我知道它可能看起来有点乱,但这似乎是我能想到的最简单的解决方案 - 当网格进入编辑模式时,在每个只读单元格上显示 TextBox:

public void DisplayEditors(DataGridView grid, DataGridViewRow row)
        {
            foreach (DataGridViewCell cell in row.Cells)
            {
                if (cell.ReadOnly == false)
                {
                    var place = grid.GetCellDisplayRectangle(cell.ColumnIndex, cell.RowIndex, true);
                    var name = string.Format("EDITOR-{0}-{1}", cell.ColumnIndex, cell.RowIndex);
                    var editor = grid.Controls.Find(name, false).FirstOrDefault();

                    if (editor == null)
                    {
                        editor = new TextBox();

                        (editor as TextBox).Name = name;

                        grid.Controls.Add(editor);
                    }
                    else
                    {
                        editor.Show();
                    }

                    editor.Size = place.Size;
                    editor.Location = place.Location;
                    editor.Text = Convert.ToString(cell.Value);
                }
            }
        }

【讨论】:

    猜你喜欢
    • 2015-01-11
    • 1970-01-01
    • 1970-01-01
    • 2011-12-24
    • 1970-01-01
    • 1970-01-01
    • 2011-05-15
    • 2021-04-20
    • 1970-01-01
    相关资源
    最近更新 更多