MultiRow Windows Forms > Developer's Guide > Using MultiRow > Cell Types > ComboBoxCell > How to Use SelectedIndexChanged Event (ComboBoxCell) |
In the combo box cell, there are two ways to perform processing (similar to the ComboBox.SelectedIndexChanged event). One is by implementing the GcMultiRow.CellEditedFormattedValueChanged event and another is by using the ComboBoxEditingControl.SelectedIndexChanged event. Implementation through the GcMultiRow.CellEditedFormattedValueChanged event is recommended.
This example uses the CellEditedFormattedValueChanged event.
Imports GrapeCity.Win.MultiRow Private Sub GcMultiRow1_CellEditedFormattedValueChanged(ByVal sender As System.Object, ByVal e As GrapeCity.Win.MultiRow.CellEditedFormattedValueChangedEventArgs) Handles GcMultiRow1.CellEditedFormattedValueChanged Dim gcMultiRow As GcMultiRow = TryCast(sender, GcMultiRow) Dim currentCell As Cell = gcMultiRow.Rows(e.RowIndex).Cells(e.CellIndex) If TypeOf currentCell Is ComboBoxCell Then Console.WriteLine("{0},{1}", e.Reason.ToString(), currentCell.EditedFormattedValue) End If End Sub |
using GrapeCity.Win.MultiRow; private void gcMultiRow1_CellEditedFormattedValueChanged(object sender, GrapeCity.Win.MultiRow.CellEditedFormattedValueChangedEventArgs e) { GcMultiRow gcMultiRow = sender as GcMultiRow; Cell currentCell = gcMultiRow.Rows[e.RowIndex].Cells[e.CellIndex]; if (currentCell is ComboBoxCell) { Console.WriteLine("{0},{1}", e.Reason.ToString(), currentCell.EditedFormattedValue); } } |
When using the ComboBoxEditingControl.SelectedIndexChanged event, you will need to use an implementation that is different from the TextBoxEditingControlTextChanged event, in order to avoid catching events that are fired during cell initialization.
This example uses the SelectedIndexChanged event.
Imports GrapeCity.Win.MultiRow Private _comboBoxEditingControl1 As ComboBoxEditingControl = Nothing Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim comboBoxCell1 As New ComboBoxCell() comboBoxCell1.Name = "ComboBoxCell1" comboBoxCell1.Items.AddRange("A", "B", "C") Dim labelCell1 As New LabelCell() labelCell1.Name = "LabelCell1" Dim labelCell2 As New LabelCell() labelCell2.Name = "LabelCell2" GcMultiRow1.Template = Template.CreateGridTemplate(New Cell() {comboBoxCell1, labelCell1, labelCell2}) End Sub Private Sub GcMultiRow1_EditingControlShowing(ByVal sender As System.Object, ByVal e As GrapeCity.Win.MultiRow.EditingControlShowingEventArgs) Handles GcMultiRow1.EditingControlShowing ' Adding cell edit control event. If TypeOf e.Control Is ComboBoxEditingControl Then Me._comboBoxEditingControl1 = TryCast(e.Control, ComboBoxEditingControl) AddHandler Me._comboBoxEditingControl1.SelectedIndexChanged, AddressOf Me._comboBoxEditingControl1_SelectedIndexChanged End If End Sub ' Event of cell edit control (Same as ComboBox.SelectedIndexChanged) Private Sub _comboBoxEditingControl1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Dim comboBox As ComboBoxEditingControl = TryCast(sender, ComboBoxEditingControl) comboBox.GcMultiRow.CurrentRow.Cells("LabelCell1").Value = comboBox.Items(comboBox.SelectedIndex).ToString() comboBox.GcMultiRow.CurrentRow.Cells("LabelCell2").Value = comboBox.SelectedIndex End Sub Private Sub GcMultiRow1_CellEndEdit(ByVal sender As System.Object, ByVal e As GrapeCity.Win.MultiRow.CellEndEditEventArgs) Handles GcMultiRow1.CellEndEdit ' Delete the event of cell edit control. If Me._comboBoxEditingControl1 Is Nothing Then RemoveHandler Me._comboBoxEditingControl1.SelectedIndexChanged, AddressOf Me._comboBoxEditingControl1_SelectedIndexChanged Me._comboBoxEditingControl1 = Nothing End If End Sub |
using GrapeCity.Win.MultiRow; private ComboBoxEditingControl _comboBoxEditingControl1 = null; private void Form1_Load(object sender, System.EventArgs e) { ComboBoxCell comboBoxCell1 = new ComboBoxCell(); comboBoxCell1.Name = "comboBoxCell1"; comboBoxCell1.Items.AddRange("A", "B", "C"); LabelCell labelCell1 = new LabelCell(); labelCell1.Name = "labelCell1"; LabelCell labelCell2 = new LabelCell(); labelCell2.Name = "labelCell2"; GcMultiRow1.Template = Template.CreateGridTemplate(new Cell[] {comboBoxCell1, labelCell1, labelCell2}); } private void gcMultiRow1_EditingControlShowing(object sender, GrapeCity.Win.MultiRow.EditingControlShowingEventArgs e) { // Adding cell edit control event. if (e.Control is ComboBoxEditingControl) { this._comboBoxEditingControl1 = e.Control as ComboBoxEditingControl; this._comboBoxEditingControl1.SelectedIndexChanged += new EventHandler(_comboBoxEditingControl1_SelectedIndexChanged); } } // Event of cell edit control (Same as ComboBox.SelectedIndexChanged) private void _comboBoxEditingControl1_SelectedIndexChanged(object sender, EventArgs e) { ComboBoxEditingControl comboBox = sender as ComboBoxEditingControl; comboBox.GcMultiRow.CurrentRow.Cells["labelCell1"].Value = comboBox.Items[comboBox.SelectedIndex].ToString(); comboBox.GcMultiRow.CurrentRow.Cells["labelCell2"].Value = comboBox.SelectedIndex; } private void gcMultiRow1_CellEndEdit(object sender, GrapeCity.Win.MultiRow.CellEndEditEventArgs e) { // Delete the event of cell edit control if (this._comboBoxEditingControl1 != null) { this._comboBoxEditingControl1.SelectedIndexChanged -= new EventHandler(_comboBoxEditingControl1_SelectedIndexChanged); this._comboBoxEditingControl1 = null; } } |