简体   繁体   中英

Add column of buttons to right hand side of DataGridView in C# or VB.NET

I would like to add a column of buttons to both sides of a DataGridView in a WinForms application written in either C# or VB.NET.

Each button is associated with a row and moves with the row as follows:

  1. Buttons on both sides are always visible, no matter how wide the view gets. So even if the user scrolls left or right the buttons stay fixed in place and always visible
  2. As the user scrolls up and down in the grid the buttons scroll with their associated row.

I thought of adding columns to the DataGridView and freezing them (eg dataGridView1.Columns[0].Frozen = true ) but the problem there is I cannot freeze both the first and last columns in the grid.

Is there another way you can think of to have a dynamic set of buttons either side of a grid?

If you want that first and all columns always visible in datagridview , then use

DataGridView.AutoSizeColumnMode = Fill

Of course width of middle columns will be changed automatically...

Or here some another workaround of your problem:

In your DataGridView create a first column with buttons("Left button") and frozen that.

Then on the right side from your Main DataGridView create another DataGridView (will call it dgvRightButton )

Add one DatGridViewButtonColumn . Then synchronize a scroll events of this two datagridview

VB.NET code

Private Sub dgvTest1_Scroll(sender As Object, e As ScrollEventArgs) Handles dgvMain.Scroll
    If e.ScrollOrientation = ScrollOrientation.VerticalScroll Then
        Me.dgvRightButton.FirstDisplayedScrollingRowIndex = Me.dgvMain.FirstDisplayedScrollingRowIndex
    End If
End Sub

Testing code I used for second datagridview (still VB.NET code, but comments are C#):

//dgvRightButton_ButtonColumn - Buttons column instance(predefined column with Designer)
//dgvMain_SomeValueColumn - predefined column from Main DataGridView

Private Sub dgvRightButton_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvLeftButton.CellClick
    //Check if column are buttons column(maybe you want add more columns later)
    If Me.dgvRightButton_ButtonColumn.Name = Me.dgvRightButton.Columns(e.ColumnIndex).Name Then
        //Getting value from Main DataGridView by rowIndex
        Dim sValue As String = Me.dgvMain.Rows(e.RowIndex).Cells(Me.dgvMain_SomeValueColumn.Name).Value
        MessageBox.Show(sValue)
    End If
End Sub

Adding same quantity of buttons to second DataGridView will be as:

Private Sub dgvRightButton_AddRows()
    For i As Int32 = 0 To Me.dgvTest1.Rows.Count - 1
        Me.dgvRightButton.Rows.Add(New String() {"Right"})
    Next
End Sub

Further to Fabio's answer I wanted to give my own findings having implemented the same approach.

I added two DataGridView components called dgvMain and dgvDelete . The latter is positioned immediately to the right of the former and is where the buttons will appear.

For dvgDelete it is positioned 18 pixels below dgvMain and its height is 34 pixels smaller. This ensures it scrolls consistently with dgvMain . I gave it a vertical scrollbar but not a horizontal.

On dgvMain I removed the vertical scrollbar.

I bound both to the same datasource (in my case an array) but prevented dgvDelete from generating columns automatically:

dgvDelete.AutoGenerateColumns = false;

I added one column to dgvDelete which contains buttons.

Similar to Fabio I added the following code (C#) to handle the scrolling.

private void dgvDelete_Scroll(object sender, ScrollEventArgs e)
{
    if (e.ScrollOrientation == ScrollOrientation.VerticalScroll)
    {
        dgvMain.FirstDisplayedScrollingRowIndex = dgvDelete.FirstDisplayedScrollingRowIndex;                
    }
}

I added a column of buttons to dgvMain and froze the first column:

dgvMain.Columns[0].Frozen = true;

So now I have a column of buttons fixed on either side of the editable data.

A nice feature of binding both grids to the same datasource is that as you move around the dgvMain grid the button in the corresponding row in dvgDelete is automatically highlighted for you.

I might even add a third grid immediately to the left of dgvMain for the first column of buttons but only if the performance impact is negligible.

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