In the previous blog, we learned how to place drop down inside DataGridView cell on cell click, and binding of dropdown with datatable.
Now, we will see how to get Selected Values of Combobox from DataGridView.
Step 1: Register handler of “SelectedIndexChange” with dropdown of DataGridView.
- ComboBox editGridCellComboBox = new ComboBox();
-
-
-
-
-
- private void dgv_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
- {
- editGridCellComboBox = (ComboBox) e.Control;
- if ((editGridCellComboBox != null)) {
- editGridCellComboBox.SelectedIndexChanged += editGridCellComboBox_SelectedIndexChanged;
- }
- }
Step 2: Create event handler method “editGridCellComboBox_SelectedIndexChanged”.
- private void editGridCellComboBox_SelectedIndexChanged(System.Object sender, System.EventArgs e)
- {
- if (editGridCellComboBox.SelectedItem != null) {
- editGridCellComboBox.SelectedIndexChanged -= editGridCellComboBox_SelectedIndexChanged;
- if (dgv.Columns[dgv.CurrentCell.ColumnIndex].Name.Equals("Description")) {
- string Description = ((DataRowView) editGridCellComboBox.SelectedItem).Row["Description"].ToString();
- string Type = ((DataRowView) editGridCellComboBox.SelectedItem).Row["Type"].ToString();
- }
- if (dgv.Columns[dgv.CurrentCell.ColumnIndex].Name.Equals("PaidWith")) {
- string PaidWith = ((DataRowView) editGridCellComboBox.SelectedItem).Row["PaidWith"].ToString();
- string Code = ((DataRowView) editGridCellComboBox.SelectedItem).Row["Code"].ToString();
- }
- editGridCellComboBox.SelectedIndexChanged += editGridCellComboBox_SelectedIndexChanged;
- }
- }
Note: The following lines are used to bring DataGridView and its dropdown in edit mode, in a single cell click.
- dgv.BeginEdit(false);
- editGridCellComboBox.DropDownHeight = 170;
- editGridCellComboBox.DroppedDown = true;
In the next blog, we will learn how to set hyperlink on email and weblink columns of DataGridView with the help of datagridviewcell style.