如果您希望第一个并且所有列始终可见datagridview
,请使用
DataGridView.AutoSizeColumnMode = Fill
当然中间列的宽度会自动改变......
或者在这里您的问题的另一种解决方法:
在您DataGridView
创建带有按钮(“左按钮”)的第一列并将其冻结。
然后在您的 Main 右侧DataGridView
创建另一个DataGridView
(将调用它dgvRightButton
)
添加一个DatGridViewButtonColumn
。然后同步这两个的一个滚动事件datagridview
VB.NET 代码
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
我第二次使用的测试代码datagridview
(仍然是 VB.NET 代码,但注释是 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
将相同数量的按钮添加到第二个DataGridView
将是:
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