【问题标题】:Adding button to DataGridView将按钮添加到 DataGridView
【发布时间】:2017-08-15 23:01:06
【问题描述】:

我正在尝试自定义 DataGridView 类以在所有行下方都有一个按钮。

到目前为止,我已经向 DataGridView.Controls 添加了一个按钮。

此按钮的位置是在每个添加/删除行、DataGridView 调整大小和滚动时计算的。

这可行,但是有一个问题。在 DataGridView 调整大小或滚动时,当 DataGridView 的底部边缘直接位于最后一行下方时,按钮完全不可见或仅部分不可见。

有没有办法让按钮始终可见?

我尝试设置滚动条位置和 FirstDisplayedScrollingRowIndex。这不起作用。 不幸的是,这个项目不可能添加一个全新的行。

添加按钮:

buttonAddRow.Height = 17;
buttonAddRow.Text = "+";
buttonAddRow.FlatStyle = FlatStyle.System;
buttonAddRow.Font = new Font(buttonAddRow.Font.FontFamily, 6.75F);
buttonAddRow.Click += ButtonAddRow_Click;
dataGridView.Controls.Add(buttonAddRow);

还有地点:

private void setLocation()
{
    if (dataGridView.FirstDisplayedCell != null)
    {
        int positionY = 0;
        positionY += dataGridView.ColumnHeadersHeight;

        var visibleRowsCount = dataGridView.DisplayedRowCount(true);
        var firstDisplayedRowIndex = dataGridView.FirstDisplayedCell.RowIndex;
        var lastvisibleRowIndex = (firstDisplayedRowIndex + visibleRowsCount) - 1;
        for (int rowIndex = firstDisplayedRowIndex; rowIndex <= lastvisibleRowIndex; rowIndex++)
        {
            positionY += dataGridView.Rows[rowIndex].Height;
        }

        buttonAddRow.Location = new Point(dataGridView.ClientRectangle.X, dataGridView.ClientRectangle.Y + positionY);
        buttonAddRow.Visible = true;
    }
}

【问题讨论】:

  • 按钮必须在最后一个网格行下或网格底部?
  • 你不需要把DataDridView弄高一点吗?
  • 好吧,你怎么能在最后一行下面显示按钮???如果您的 dgv 充满了行,这似乎不合逻辑.. 可能的解决方法:使最后一行是正常高度的两倍..
  • @Ash - 在最后一个网格行下。如果只有几行,则垂直滚动不可见,最后一行是例如在网格的中间,按钮也会在网格的中间。
  • @stuartd - 如果我让网格更高,将显示下一个未显示的 FirstDisplayedRow 行,并且按钮也会向下。如果我可以增加 DataGridView 底部边缘和最后一行之间的空间,这将起作用。不幸的是,我没有找到方法。

标签: c# .net winforms datagridview


【解决方案1】:

下面是一些代码,它创建了我之前描述的“按钮行”,并将此按钮行添加到DataGridView 的顶部、底部和第 4 行。如图所示,此按钮仅位于第一列。如果要在所有列中显示按钮,则必须实现 OnPaint 方法来调整该行。但是,查看图片,如果用户向下滚动,则顶部按钮行将不可见,这是您必须在用户向下滚动时将按钮行保持在顶部的地方。如果这是您要查找的内容,那么您最终会得到一个始终显示在网格顶部的 STATIC 按钮。再次将此按钮放在网格上方和外部将完成同样的事情,而且工作量要少得多。

下面的代码使用带有 3 个文本列的 DataGridView

private void Form1_Load(object sender, EventArgs e) {
  FillGrid();
  InsertButtonRow(0);
  InsertButtonRow(4);
  InsertButtonRow(dataGridView.Rows.Count -1);
}

private void FillGrid() {
  for (int i = 1; i < 15; i++) {
    dataGridView.Rows.Add("Row" + i + "C1", "Row" + i + "C2", "Row" + i + "C3");
  }
}

private void InsertButtonRow(int rowIndex) {
  if (rowIndex >= 0 && rowIndex < dataGridView.Rows.Count) {
    DataGridViewButtonCell buttonCell = new DataGridViewButtonCell();
    buttonCell.Value = "+";
    buttonCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
    DataGridViewRow row = (DataGridViewRow)dataGridView.Rows[0].Clone();
    row.Cells[0] = buttonCell;
    dataGridView.Rows.Insert(rowIndex, row);
  }
}

【讨论】:

  • 谢谢约翰。非常感谢你的例子。是的,我知道将按钮放在单元格中。但这又是我想避免的同一个问题——添加一个新的额外行。如果我想将按钮放在最后一行下方,我需要添加没有单元格边框的额外行并将按钮放在单元格中。在这种情况下,我需要检查遍历行的所有逻辑。如果将按钮添加到 dgv.Controls,则行保持不变,无需检查任何逻辑。无论如何,再次感谢您的帮助。
  • 查看链接中的 devexpress 示例,网格似乎具有此功能,您可以将“新项目行”放置在网格可见部分的顶部或底部。它显然是某种“插入”行。我想我要表达的主要观点是……如果网格充满了行……你可以在哪里添加这个按钮而不掩盖一些东西?链接中的示例似乎使用行。即使新行位于顶部,它也可能“显示”为一个按钮,但我猜在幕后……它是一行。
猜你喜欢
  • 2014-02-07
  • 2011-12-16
  • 2022-12-28
  • 1970-01-01
  • 1970-01-01
  • 2013-09-27
  • 1970-01-01
  • 1970-01-01
  • 2021-06-11
相关资源
最近更新 更多