A DataGridView control won't bind directly to an IEnumerable object, so two options are:
1. Create a BindingSource, set its DataSource to the IEnumerable object, then set the DataGridView.DataSource property to the BindingSource.
2. Bind the DataGridView.DataSource property to the IEnumerable object's ToList() method.
Thanks to Ken Tucker on Microsoft Forums for this tip.
Showing posts with label DataGridView. Show all posts
Showing posts with label DataGridView. Show all posts
Thursday, April 3, 2008
Wednesday, August 15, 2007
Turning off auto-scrolling in a bound DataGridView
I was running into an issue where I didn't want the grid to auto-scroll or change the selected row when the underlying DataSource property was changed. Here's what I came up with:
' dgv is a DataGridView control with SelectionMode set to FullRowSelect and MultiSelect set to False.
Dim intSelectedIndex As Integer = 0
If dgv.SelectedRows.Count > 0 Then
intSelectedIndex = dgv.SelectedRows(0).Index
End If
Dim intDisplayIndex As Integer = dgv.FirstDisplayedScrollingRowIndex
' Set dgv.DataSource here
dgv.ClearSelection()
If intSelectedIndex < dgv.Rows.Count Then
dgv.Rows(intSelectedIndex).Selected = True
Else
dgv.Rows(dgv.Rows.Count - 1).Selected = True
End If
If intDisplayIndex > -1 Then
If intDisplayIndex <>
dgv.FirstDisplayedScrollingRowIndex = intDisplayIndex
Else
dgv.FirstDisplayedScrollingRowIndex = dgv.Rows.Count - 1
End If
End If
' dgv is a DataGridView control with SelectionMode set to FullRowSelect and MultiSelect set to False.
Dim intSelectedIndex As Integer = 0
If dgv.SelectedRows.Count > 0 Then
intSelectedIndex = dgv.SelectedRows(0).Index
End If
Dim intDisplayIndex As Integer = dgv.FirstDisplayedScrollingRowIndex
' Set dgv.DataSource here
dgv.ClearSelection()
If intSelectedIndex < dgv.Rows.Count Then
dgv.Rows(intSelectedIndex).Selected = True
Else
dgv.Rows(dgv.Rows.Count - 1).Selected = True
End If
If intDisplayIndex > -1 Then
If intDisplayIndex <>
dgv.FirstDisplayedScrollingRowIndex = intDisplayIndex
Else
dgv.FirstDisplayedScrollingRowIndex = dgv.Rows.Count - 1
End If
End If
Labels:
data bound,
DataGridView,
DataSource,
scrolling,
VB.NET
Subscribe to:
Posts (Atom)