C#Winform:内联datagridview编辑

时间:2016-02-19 18:47:45

标签: c# winforms datagridview

最初我的datagridview看起来如下

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

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

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

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

您可以将网格上的EditMode设置为EditOnEnter,以便在单击时将单元格置于编辑模式。

您仍然需要在DataGridView外部处理保存功能。