简体   繁体   中英

Adding button to DataGridView

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

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

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

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?

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

按钮部分可见

Adding button:

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

And the location:

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

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.

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM