【问题标题】:Adding a Command Column to DataGridView向 DataGridView 添加命令列
【发布时间】:2015-08-29 08:07:42
【问题描述】:

我有一个带有一些列的 c# DataGridView。我想添加一个操作列作为每行中的最后一个单元格。

每一行都应该有查看、删除、更新等按钮或图标。

【问题讨论】:

  • 您必须为 each 命令添加DataGridViewButtonColumn 类型的列。这里和这里有很多例子......
  • 谢谢,但我认为我的问题有点不同,我需要添加多个按钮,即一个按钮面板(如更新、删除、编辑、查看)我如何添加一个按钮面板,每个按钮都有不同的动作。
  • 不,你不能轻易做到这一点。您只是不能将 控件 添加到单元格中。您可以启动herehere 来创建自定义单元格类型,但要准备好为一些微不足道的按钮做大量工作!显然不值得imo ..为每个按钮添加一个列,你的好! ——
  • 引用一段话:DataGridView 控件提供了多种列类型,使您的用户能够以多种方式输入和编辑值。但是,如果这些列类型不能满足您的数据输入需求,您可以使用承载您选择的控件的单元格创建自己的列类型。为此,您必须定义派生自 DataGridViewColumn 和 DataGridViewCell 的类。您还必须定义一个派生自 Control 并实现 IDataGridViewEditingControl 接口的类。

标签: c# .net winforms datagridview


【解决方案1】:

以下是我能想到的几个选项:

  • 到目前为止,最简单的方法是为每个 Button 添加一个Column。 (推荐!
  • 你可以创建一个自定义单元格类型,也许是一个Panel 子类和你想要的Buttons。这涉及实现IDataGridViewEditingControl iterface 至少有十几个字段和方法,以及两个从DataGridViewColumnDataGridViewCell 派生的自定义类,还有更多事情要做。简而言之,大量工作!对于一个复杂的编辑控件来说可能是值得的。对于一些Buttons 肯定不是! (不推荐!
  • 或者您可以在CellPainting 事件中通过一点所有者绘图魔法伪造Buttons。见下文..!
  • 或者您可以在作用于当前行的DataGridView外部添加一个复杂的控件。 通常的方式!

这是一个有趣的小例子,所有者在DatagGridView DGV 的第四个Column 中绘制了四个“SIDU”命令:

private void Form1_Load(object sender, EventArgs e)
{
    DGV.Rows.Add(12);
    for( int i = 0; i< DGV.Rows.Count; i++)
    {
        DGV[0, i].Value = i;
        DGV[1, i].Value = R.Next(1000);
        DGV[2, i].Value = rights[R.Next(rights.Count)];
        DGV[3, i].ReadOnly = true;
    }
}

List<string> rights = new List<string> 
  { "SIDU", "SID-", "SI-U", "S-DU", "SI--", "S--U", "S-D-", "S---" };
Dictionary<char, string> rightsTexts = new Dictionary<char, string> 
  { { 'S', "Select" }, { 'I', "Insert" }, { 'D', "Delete" }, { 'U', "Update" } };
Random R = new Random(1);


private void DGV_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.ColumnIndex == 3 && e.RowIndex >= 0)
    {
        string r = DGV[2,e.RowIndex].Value.ToString();
        StringFormat format = new StringFormat()
        { LineAlignment = StringAlignment.Center,  Alignment = StringAlignment.Center};

        int w = e.CellBounds.Width / 4;
        int y = e.CellBounds.Y + 1;
        int h = e.CellBounds.Height - 2;

        e.PaintBackground(e.CellBounds, false);

        for (int i = 0; i < 4; i++)
        {
           int x = e.CellBounds.X + i * w;
           Rectangle rect = new Rectangle(x, y, w, h);
           ControlPaint.DrawButton(e.Graphics, rect, ButtonState.Normal);
           if (rightsTexts.ContainsKey(r[i])) 
               e.Graphics.DrawString(rightsTexts[r[i]], DGV.Font,
                                     SystemBrushes.WindowText, rect ,format );
        }
        e.Handled = true;
    }
}

private void DGV_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
    if (e.ColumnIndex == 3 && e.RowIndex >= 0)
    {
        DataGridViewCell cell = DGV[e.ColumnIndex, e.RowIndex];
        int w = cell.Size.Width;
        int buttonIndex = e.X * 4 / w;
        Text = rightsTexts.ElementAt(buttonIndex).Value;
    }
}

绘图的东西被扔掉了,所以你可以花更多的精力来微调它。..

我已选择在可见单元格中显示权限以进行演示。对于生产来说,action cell 的价值是显而易见的。

【讨论】:

    猜你喜欢
    • 2023-03-30
    • 2020-10-09
    • 1970-01-01
    • 1970-01-01
    • 2014-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    相关资源
    最近更新 更多