Displaying a ComboBox in Cell
Frequently, besides displaying text in a cell you may want to display a drop-down list box to allow users to select from a list of pre-determined values. In this case, you need to add a ComboBox to the cells in the desired column. The following code snippet adds a ComboBox control to the fifth column of the DataGridView control:
Frequently, besides displaying text in a cell you may want to display a drop-down list box to allow users to select from a list of pre-determined values. In this case, you need to add a ComboBox to the cells in the desired column. The following code snippet adds a ComboBox control to the fifth column of the DataGridView control:
'---add columns to the DataGridView control---
DataGridView1.Columns.Add("ID", "Product ID")
DataGridView1.Columns.Add("Name", "Product Name")
DataGridView1.Columns.Add("Description", "Description")
DataGridView1.Columns.Add("Price", "Price")
'---create a new bindingsource control---
Dim bindingsource As New BindingSource
'---add the items into the control---
bindingsource.Add("Type A")
bindingsource.Add("Type B")
bindingsource.Add("Type C")
'---create a combobox column---
Dim comboBoxCol As New DataGridViewComboBoxColumn
'---set the header---
comboBoxCol.HeaderText = "Types"
'---data bind it---
comboBoxCol.DataSource = bindingsource
'---add a combobox column to the DataGridView control---
DataGridView1.Columns.Add(comboBoxCol)
Figure 11 shows the output.
The previous code showed how you can bind a BindingSource control (containing a list of items to let the users choose) to a DataGridViewComboBoxColumn control inside a DataGridView control. An alternative way would be to add the items to the DataGridViewComboBoxColumn control directly, as shown:
The previous section showed how to display a ComboBox control within a cell in the DataGridView control. There are times when you need to allow the users to insert additional items into the ComboBox control. In this case, you need to do some work.
First, service the EditingControlShowing event of the DataGridView control. This event is fired when the user tries to edit the ComboBox control:
You can prevent users from modifying a particular row by setting the ReadOnly property for that particular row to True:
During runtime, you can hide a particular column by setting its Visible property to False:
Validating Users' Edit
A common use of the DataGridView control is for data entry. Very often, users will input data into the individual cells in the control. Therefore, it is important to perform validation so that the users enter the correct type of data. To validate that the data entered is of the correct type, you need to service two events. First, service the CellValidating event, which is fired when the user has finished modifying the value of a cell and leaves the cell:
|
'---adding columns---
DataGridView1.Columns.Add("ID", "Product ID")
DataGridView1.Columns.Add("Name", "Product Name")
DataGridView1.Columns.Add("Description", "Description")
DataGridView1.Columns.Add("Price", "Price")
'---add a combobox column---
Dim comboBoxCol As New DataGridViewComboBoxColumn
'---set the header text---
comboBoxCol.HeaderText = "Types"
'---add items to it---
comboBoxCol.Items.Add("Type A")
comboBoxCol.Items.Add("Type B")
comboBoxCol.Items.Add("Type C")
DataGridView1.Columns.Add(comboBoxCol)
The latter approach is more flexible as it allows the user to add new items into the drop-down list during runtime (see the next section for details). If the ComboBox control is bound to a data source, you will not be able to add new items into it during runtime. Adding Items into a DataGridViewComboBoxColumn ControlThe previous section showed how to display a ComboBox control within a cell in the DataGridView control. There are times when you need to allow the users to insert additional items into the ComboBox control. In this case, you need to do some work.
First, service the EditingControlShowing event of the DataGridView control. This event is fired when the user tries to edit the ComboBox control:
Private Sub DataGridView1_EditingControlShowing( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms. _
DataGridViewEditingControlShowingEventArgs) _
Handles DataGridView1.EditingControlShowing
Dim comboBoxColumn As DataGridViewComboBoxColumn = _
DataGridView1.Columns(4)
If (DataGridView1.CurrentCellAddress.X = _
comboBoxColumn.DisplayIndex) Then
Dim cb As ComboBox = e.Control
If (cb IsNot Nothing) Then
cb.DropDownStyle = ComboBoxStyle.DropDown
End If
End If
End Sub
Here, you essentially check to see if the cell being edited is the one containing the ComboBox. If it is, the drop-down style of the ComboBox control is set to DropDown so that the user can type into it. Next, service the CellValidating event of the DataGridView control. This event is fired when the user is done with the typing and leaves the cell:
Private Sub DataGridView1_CellValidating( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms. _
DataGridViewCellValidatingEventArgs) _
Handles DataGridView1.CellValidating
Dim comboBoxColumn As DataGridViewComboBoxColumn = _
DataGridView1.Columns(4)
If (e.ColumnIndex = comboBoxColumn.DisplayIndex) Then
If (Not comboBoxColumn.Items.Contains( _
e.FormattedValue)) Then
comboBoxColumn.Items.Add(e.FormattedValue)
End If
End If
End Sub
Here, you will perform the usual checks and then add the newly typed item into the ComboBox. Figure 12 shows that originally the ComboBox contains three items. The user can now type in a new item (e.g. "Type D") and then it will be added into the list. Note that for subsequent rows (as well as the rows above it) the ComboBox control will now contain four items. Locking Rows/ColumnsYou can prevent users from modifying a particular row by setting the ReadOnly property for that particular row to True:
'---first row is readonly---
DataGridView1.Rows(0).ReadOnly = True
Likewise, you can also lock cells belonging to a particular column:
'---first column is readonly---
DataGridView1.Columns(0).ReadOnly = True
Note that while setting the ReadOnly property to True prevents the user from editing the values in the cell, this does not affect his ability to delete rows. If you want, you can lock the entire DataGridView control:
'---entire grid is readonly---
DataGridView1.ReadOnly = True
Hiding ColumnsDuring runtime, you can hide a particular column by setting its Visible property to False:
'---hides the second column---
DataGridView1.Columns(1).Visible = False
Figure 13 shows the DataGridView before (shown in the background) and after hiding the second column (the ContactName column). |
A common use of the DataGridView control is for data entry. Very often, users will input data into the individual cells in the control. Therefore, it is important to perform validation so that the users enter the correct type of data. To validate that the data entered is of the correct type, you need to service two events. First, service the CellValidating event, which is fired when the user has finished modifying the value of a cell and leaves the cell:
Private Sub DataGridView1_CellValidating( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms. _
DataGridViewCellValidatingEventArgs) _
Handles DataGridView1.CellValidating
'---Price field---
If e.ColumnIndex = 3 Then
If Not IsNumeric(e.FormattedValue) Then
DataGridView1.Rows(e.RowIndex).ErrorText = _
"Price must be a numeric value."
e.Cancel = True
End If
End If
End Sub
In this event, you will perform the necessary validation. For example, in the above example, you will check that the fourth column (column index 3—the Price field) contains only numeric values. If not, an error message is displayed on the leftmost column of the DataGridView control (set via the ErrorText property). Figure 14 shows this in action. The error must be corrected before the user can switch to the other cells. To ensure that the error message goes away when the correct data is entered, service the CellEndEdit event, as follows:
Private Sub DataGridView1_CellEndEdit( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
Handles DataGridView1.CellEndEdit
'---clear the error message---
DataGridView1.Rows(e.RowIndex).ErrorText = String.Empty
End Sub
Are you looking to earn money from your websites or blogs by popup ads?
BalasHapusIn case you are, did you know about Clickadu?