向DataGridView添加按钮 [英] Adding button to DataGridView

查看:101
本文介绍了向DataGridView添加按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to customize DataGridView class to have a button beneath all rows.

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

So far I've added a button to a DataGridView.Controls.

在每个添加/删除行上计算此按钮的位置,DataGridView调整大小并滚动.

The position of this button is calculated on each add/remove row, DataGridView resize and scroll.

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

This works, however there is a one problem with that. On DataGridView resize or scroll, when bottom edge of the DataGridView is directly below last row, the button is not visible at all or just partially.

是否有办法使按钮始终可见?

Is there a way to make the button always visible?

我尝试设置滚动条位置和FirstDisplayedScrollingRowIndex.这是行不通的.不幸的是,此项目无法添加全新的行.

I've tried setting scrollbar position and FirstDisplayedScrollingRowIndex. This does not work. Unfortunatelly adding a whole new row isn't possible for this project.

添加按钮:

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

推荐答案

下面是一些代码,该代码创建了我前面介绍的按钮行",并将此按钮行添加到了 DataGridView的顶部,底部和第4行.如图所示,此按钮仅在第一列中.如果要在所有列上显示按钮,则必须实现OnPaint方法来调整此行.但是,从图片上看,如果用户向下滚动,则顶部的按钮行将不可见,这是您必须在用户向下滚动时实现将按钮行保持在顶部的位置.如果这是您要查找的内容,那么最终得到的是一个STATIC按钮,该按钮始终显示在网格顶部.再次在网格上方和外部放置此按钮,将花费更少的精力完成相同的事情.

Below is some code that creates a "button Row" I described earlier and adds this button row to the top, bottom and row 4 of the DataGridView. As the picture shows this button is only in the first column. If you want to display the button across all columns then you will have to implement the OnPaint method to adjust this row. However, looking at the picture, if the user scrolls down then the top button row will not be visible, this is where you would have to implement keeping the button row at the top as the user scrolled down. If this is what you are looking for, then what you end up with is a STATIC button that is always displayed at the top of the grid. Again putting this button ABOVE and OUTSIDE the grid would accomplish the same thing, with much less effort.

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

The code below uses a DataGridView with 3 text columns.

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

这篇关于向DataGridView添加按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆