User FAQs

1) How can I remove all the rows ?


2) How can I remove the gridlines ?
Set the CellBorderStyle to None

3) How can I always display the vertical scrollbar ?
By default the datagridview only displays scrollbars when they are needed.
If you do want scrollbars permanently displayed then you need to derive your own class.

4) How can I get the value in the first column ?
The first column is at cell position 0.


5) How can I display the scrollbars after its been disabled ?
If you set the Enabled property to False to disable a datagridview and then later change it back to True, the scrollbars are not displayed.
try repopulating the datagridview or don't disable this control

6) How can I maintain the scroll position ?

Me.dgrDataGrid.CurrentCell = 

7) How can I display a string date column in a specific date format ?
You must use MMMM and not mmmm (lowercase)

Private Sub dgvDataGridView_CellFormatting(ByVal sender As Object, _ 
                                           ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) _
                                           Handles dgvProjects.CellFormatting

   If Me.dgvDataGridView.Columns(e.ColumnIndex).Name = "DateStart" Then
      If e.Value.ToString.Length > 0 Then
         e.Value = CType(e.Value, System.DateTime).ToString("dd MMMM yy")
      End If
   End If
End Sub

8) How can I change the Row Height ?
Expand the Row Template for the default and change the height property to 16

9) How can I select the whole row ?
Change the SelectionMode property to FullRowSelect

10) How can I get the rowindex of the currently selected cell ?


although this is no good if you are inside the SelectionChanged event, in this situation use the following line


11) How can I select a different row ?

Me.dgrDataGridView.Rows(index).Selected = True 

12) How can I be notified when a different row is selected ?
Save a form-level variable to keep track of the last selected row.

Private ilastselectedrowindex As System.Int32 

Private Sub dgrDataGridView_CurrentCellChanged(---)
   If (ilastselectedrowindex <> Me.dgrDataGridView.CurrentCell.RowIndex) Then

   End If
   ilastselectedrowindex = Me.dgrDataGridView.CurrentCell.RowIndex
End Sub

13) How can I check if a selection has been made ?

If (Me.dgrDataGridView.SelectedRows.Count > 0) Then 
End If

14) How can I remove the current selection from the datagridview, so nothing appears selected ?


15) How can I enable typing a character and moving to that specific row (auto-complete for the first character) ?
It is possible to jump to the row starting with a particular letter.
This can be achieved using the KeyDown event.

16) How can I remove the currently selected rows from the datagridview ?

Dim irowno As Integer 
For irowno = dgrDataGridView.SelectedRows.Count - 1 To 0 Step -1
   objDataRow = dgrDataGridView.SelectedRows(irowno)

Next irowno

17) How can I automatically scroll to the selected row ?


18) How can I format a whole row ?
Have to do it cell by cell

Rows().Cells(0).Style.BackColor = 

19) How can I remove rows ?


20) How can I shade the cells based on their value ?
This can be done using the CellFormatting event

21) How can I hide a column ?

dgvDataGridView.Columns(2).Visible = False 

22) How can I make the last column wide enough to occupy the rest of the grid ?
Set the AutoSizeMode for the last column to Fill
You should also set a MinimumSize

23) How can I prevent a column from being selected ?
This can be achieved by checking the column of the active cell in the CellBeginEdit event.

Private Sub dgrDataGridView_CellBeginEdit(---) 
   If (CType(sender, System.Windows.Forms.DataGridView).CurrentCell.ColumnIndex <> 1 Then
   End If
End Sub

24) How to sort a column that contains numbers
Make sure that the ValueType is set to a numeric data type

Me.dgrDataGrid.Columns(2).ValueType = System.Type.GetType("Double") 

25) How can I allow typing in a combo box cell ?
Two things have to be done.
The DropDownStyle property of the ComboBox editing control has to be changed to DropDown
Ensure that the entry made is added to the combobox collection.
The best place to add this entry is in the CellValidating event

Private Sub dgvDataGridView_CellValidating(ByVal sender As Object, _ 
                                           ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) _

   If Me.dgvDataGridView.Columns(e.ColumnIndex).Name = "ComboColumnName" Then
      If Me.dgvDataGridView.Columns(e.ColumnIndex).Items.Contains(e.FormattedValue) = False Then
      End If
   End If
End Sub

26) How can I have a combobox display a subset based on the selection in another combobox column ?
To enable this you need two versions of the filtered list (or subcategory).
Once list has no filters and the other is filtered based on the selection in the first combobox.

Private Sub dgvDataGridView_CellBeginEdit(ByVal sender As Object, _ 
                                          ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs)

'set the combobox cell datasource to the filtered Binding Source
Dim objcomboboxcell As System.Windows.Forms.DataGridViewComboBoxCell
   objcomboboxcell = CType(Me.dgvDataGridView(e.ColumnIndex, e.RowIndex) , _

   objcomboboxcell.DataSource =

End Sub

Private Sub dgvDataGridView_CellEndEdit(ByVal sender As Object, _
                                        ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)

End Sub

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext