C# Winform:内联数据网格视图编辑

本文关键字:数据网 网格 视图 编辑 数据 Winform | 更新日期: 2023-09-27 18:35:21

最初我的datagridview如下所示

ID      Name        City        Action
------  ------      ----        ------
1       Mitch       Kolkata     Edit
2       Simon       Delhi       Edit
3       Poly        Madras      Edit

因此,用户无法更改,但是当用户单击编辑按钮时,将在名称列中放置一个文本框,并将在"城市"列上放置一个城市dropdown,该行的编辑按钮将由用户单击。

当用户单击编辑按钮时,编辑按钮文本将更改为保存

,当用户单击保存按钮时,保存按钮文本将更改为编辑。 因此,请指导我在使用datagridview时如何实现在线编辑功能。 谢谢

C# Winform:内联数据网格视图编辑

你所要求的内容自然不受DataGridView的支持,所以你需要通过维护一些状态、挂钩到几个事件和设置几个属性来实现整个逻辑。

最重要的部分是设置适当的列/单元格属性。

(A) 初始设置

var colName = new DataGridViewTextBoxColumn { HeaderText = "Name" };
var colCity = new DataGridViewComboBoxColumn { HeaderText = "City" };
var colAction = new DataGridViewButtonColumn { HeaderText = "Action" };
colName.ReadOnly = true;
colCity.ReadOnly = true;
colCity.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
colAction.Text = "Edit";

(B) 进入编辑模式

var cellName = (DataGridViewTextBoxCell)row.Cells[colName.Index];
var cellCity = (DataGridViewComboBoxCell)row.Cells[colCity.Index];
var cellAction = (DataGridViewButtonCell)row.Cells[colAction.Index];
cellName.ReadOnly = false;
cellCity.ReadOnly = false;
cellCity.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;
dg.CurrentCell = cellName;
dg.BeginEdit(true);
cellAction.Value = "Save";

(C) 退出编辑模式

var cellName = (DataGridViewTextBoxCell)row.Cells[colName.Index];
var cellCity = (DataGridViewComboBoxCell)row.Cells[colCity.Index];
var cellAction = (DataGridViewButtonCell)row.Cells[colAction.Index];
cellName.ReadOnly = true;
cellCity.ReadOnly = true;
cellCity.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
cellAction.Value = "Edit";

这是一个完整的工作演示:

using System;
using System.Linq;
using System.Windows.Forms;
namespace Demos
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            var form = new Form();
            var dg = new DataGridView { Dock = DockStyle.Fill, Parent = form };
            dg.AllowUserToAddRows = dg.AllowUserToDeleteRows = false;
            var colId = new DataGridViewTextBoxColumn { HeaderText = "Id", ReadOnly = true };
            var colName = new DataGridViewTextBoxColumn { HeaderText = "Name" };
            var colCity = new DataGridViewComboBoxColumn { HeaderText = "City" };
            var colAction = new DataGridViewButtonColumn { HeaderText = "Action" };
            colName.ReadOnly = true;
            colCity.ReadOnly = true;
            colCity.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
            colAction.Text = "Edit";
            dg.Columns.AddRange(colId, colName, colCity, colAction);
            var data = new[]
            {
                new { Id = 1, Name = "Mitch", City = "Kolkata" },
                new { Id = 2, Name = "Simon", City = "Delhi" },
                new { Id = 3, Name = "Poly", City = "Madras" },
            };
            colCity.Items.AddRange(data.Select(item => item.City).Distinct().ToArray());
            foreach (var item in data)
                dg.Rows.Add(item.Id, item.Name, item.City, "Edit");
            Action<DataGridViewRow> enterEditMode = row =>
            {
                var cellName = (DataGridViewTextBoxCell)row.Cells[colName.Index];
                var cellCity = (DataGridViewComboBoxCell)row.Cells[colCity.Index];
                var cellAction = (DataGridViewButtonCell)row.Cells[colAction.Index];
                cellName.ReadOnly = false;
                cellCity.ReadOnly = false;
                cellCity.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;
                dg.CurrentCell = cellName;
                dg.BeginEdit(true);
                cellAction.Value = "Save";
            };
            Action<DataGridViewRow> exitEditMode = row =>
            {
                var cellName = (DataGridViewTextBoxCell)row.Cells[colName.Index];
                var cellCity = (DataGridViewComboBoxCell)row.Cells[colCity.Index];
                var cellAction = (DataGridViewButtonCell)row.Cells[colAction.Index];
                cellName.ReadOnly = true;
                cellCity.ReadOnly = true;
                cellCity.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
                cellAction.Value = "Edit";
            };
            dg.CellContentClick += (sender, e) =>
            {
                if (e.ColumnIndex == colAction.Index)
                {
                    var row = dg.Rows[e.RowIndex];
                    var cellAction = (DataGridViewButtonCell)row.Cells[colAction.Index];
                    if ((string)cellAction.Value == "Edit")
                        enterEditMode(row);
                    else if (dg.EndEdit())
                    {
                        // Save code goes here ...
                        exitEditMode(row);
                    }
                }
            };
            dg.RowValidated += (sender, e) =>
            {
                var row = dg.Rows[e.RowIndex];
                var cellAction = (DataGridViewButtonCell)row.Cells[colAction.Index];
                if ((string)cellAction.Value == "Save")
                    exitEditMode(row);
            };
            Application.Run(form);
        }
    }
}

可以将网格上的"编辑模式"设置为"编辑输入",以便在单击单元格时将其置于编辑模式。

您仍然必须在 DataGridView 的外部处理您的保存函数。