Hi,
I'm creating a datagrid in code, setting the datasource to a datatable object. I don't need to write back the data to the datatable object, I'm having issues using combo box controls on the datagrid.
DataGridView dg = new DataGridView();Before I set the datasource of the datagridview to the datatable object, I change some of the columns to be DataGridViewComboBoxColumn control types by calling the following method for each of the columns I need to be combo boxes.
private void GenerateComboboxes(DataTable comboDataSource, string name, string headerText, string dataPropertyName, string valueMember, string displayMember) { DataView theDataView = new DataView(comboDataSource); DataGridViewComboBoxColumn colCombo = new DataGridViewComboBoxColumn(); colCombo.Name = name; colCombo.HeaderText = headerText; colCombo.DataPropertyName = dataPropertyName; colCombo.ValueMember = valueMember; colCombo.DisplayMember = displayMember; colCombo.DataSource = theDataTable; colCombo.ValueType = typeof(long); dg.Columns.Add(colCombo); }
I then run the following, and I end up with a working datagrid.
dg.AutoGenerateColumns = true; dg.DataSource = dt; //Populated DataTable object dg.Name = "theDataGrid"; EnquiryForm.GetControl("Frame1").Controls.Add(dg); dg.Dock = DockStyle.Fill; dg.AllowUserToAddRows = false; dg.AllowUserToDeleteRows = false; dg.AllowUserToOrderColumns = false; dg.AllowUserToResizeRows = false; dg.EditMode = DataGridViewEditMode.EditOnEnter;
dg.EditingControlShowing += dg_EditingControlShowing;
2 of the columns are combo boxes, and when entering the cell and modifying the value, the other combo box will update. To achieve this I've hooked into the events below. The columns in question are AccountName and AccountNumber. Both have a Value of "AccountID"
private void dg_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { if(e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl)) { ComboBox combo = e.Control as ComboBox; if (combo != null) { combo.SelectedIndexChanged += new EventHandler(comboChanged_SelectedIndexChanged); combo.Leave += new EventHandler(RemoveComboValueChangedHandler); } } private void RemoveComboValueChangedHandler(object sender, EventArgs e) { ComboBox cb = (ComboBox)sender; cb.SelectedIndexChanged -= new EventHandler(comboChanged_SelectedIndexChanged); } private void comboChanged_SelectedIndexChanged(object sender, EventArgs e) { if(dg.CurrentCell.ColumnIndex == dg.Columns["AccountName"].Index) { ComboBox cbAccountName = (ComboBox)sender; if(cbAccountName.SelectedValue == DBNull.Value || cbAccountName.SelectedValue == null) { dg.CurrentRow.Cells["AccountNumber"].Value = DBNull.Value; } else { dg.CurrentRow.Cells["AccountNumber"].Value = cbAccountName.SelectedValue; } } else if(dg.CurrentCell.ColumnIndex == dg.Columns["AccountNumber"].Index) { ComboBox cbAccountNumber = (ComboBox)sender; if(cbAccountNumber.SelectedValue == DBNull.Value || cbAccountNumber.SelectedValue == null) { dg.CurrentRow.Cells["AccountName"].Value = DBNull.Value; } else { dg.CurrentRow.Cells["AccountName"].Value = cbAccountNumber.SelectedValue; } } }
The SelectedIndex event works fine, and the other combo box updates. But, if I am am in the last row of the datagrid and change the value and then press Enter, I get the following message
http://imgur.com/atoWmQG
When I press Enter on the final Row, I want the row to 'commit'. Does anyone have any idea why this error is happening and how I can stop it?
thanks