C# Winform:内联 datagridview 编辑

Mou*_*Mou -3 c# datagridview winforms

最初我datagridview看起来像下面

ID      Name        City        Action
------  ------      ----        ------
1       Mitch       Kolkata     Edit
2       Simon       Delhi       Edit
3       Poly        Madras      Edit
Run Code Online (Sandbox Code Playgroud)

所有数据都将是只读格式。所以用户不能改变,但是当用户点击编辑按钮时,一个文本框将被放置在名称列中,一个城市dropdown将被放置在城市列上的一行,其编辑按钮将被用户单击。

当用户单击编辑按钮时,编辑按钮文本将更改为保存,当用户单击保存按钮时,保存按钮文本将更改为编辑。所以指导我如何在使用datagridview. 谢谢

Iva*_*oev 5

您所要求的不是 自然支持的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";
Run Code Online (Sandbox Code Playgroud)

(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";
Run Code Online (Sandbox Code Playgroud)

(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";
Run Code Online (Sandbox Code Playgroud)

这是一个完整的演示:

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)