【问题标题】:C# Winform: Inline datagridview edit [closed]C# Winform:内联 datagridview 编辑
【发布时间】:2016-02-19 18:47:44
【问题描述】:

最初我的datagridview 如下所示

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

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

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

【问题讨论】:

  • 自己在谷歌上快速搜索一下如何Update DataGridView using ItemTemplate,您希望将编辑按钮作为网格的一部分.. 有很多关于如何做到这一点的工作示例.. 请展示你付出了更多的努力......你说initially my datagridview would look like意味着你还没有做任何事情......?你能告诉我们你到目前为止自己尝试过的东西吗..
  • 我在 C# winform 模板中使用 DataGridView。我想没有像 ItemTemplate 这样的概念。 ItemTemplate 在 asp.net webform 的 gridview 中。

标签: c# winforms datagridview


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 我不想将所有行都置于编辑模式,而是想编辑用户单击编辑按钮的行。那么如何以编程方式在编辑模式下设置特定行?可能吗。你介意提供一些示例代码吗?
    • 然后编写代码,从单击按钮中获取当前选定的行...向我们展示您自己编写或尝试过的代码
    • @Mou 请告诉我你到目前为止有什么,我可以帮助你重构它以满足你的需要。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-09
    • 1970-01-01
    • 1970-01-01
    • 2019-02-21
    • 1970-01-01
    • 2022-08-19
    相关资源
    最近更新 更多