DataGridViewComboBoxCell.DataSource is set to null when the form is displayed.

I add a DataGridViewComboBoxColumn to the DataGridView during the Load form handler and setting the DataSource of each DataGridViewComboBoxCell in the columns. However, once the form is shown, the DataSource of each DataGridViewComboBoxCell been set to null . Here is the code that I use to populate a column and its cells:

 DataGridViewComboBoxColumn comboCol; comboCol = new DataGridViewComboBoxColumn(); comboCol.Name = "ComboCol"; comboCol.HeaderText = "Combo Column"; comboCol.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; this.dgv.Columns.Add(comboCol); for (int i = 0; i < dgv.Rows.Count; i++) { // This datatable is actually populated here! DataTable myData = PopulatedDataTable(dgv.Rows[i].Cells["info"].Value); DataGridViewComboBoxCell DCC = new DataGridViewComboBoxCell(); DCC = (DataGridViewComboBoxCell)dgv.Rows[i].Cells["CombolCol"]; DCC.DataSource = myData; DCC.DisplayMember = "Association"; // Association is a column in myData DCC.ValueMember = "Association"; } dgv.Columns["association"].Visible = false; 

This code does exactly what is expected if I put it with the button, which I press AFTER the form loads, but when executed while the form is loading, the DataSource is cleared. Any suggestions?

+4
source share
3 answers

As a result, I solved this problem to handle the DataBindingComplete event in the datagrid.

Apparently, when you populate the datagrid during the event of the Load form, the data in the datagrid is restored. This twisted the data in an unrelated column that I was trying to add.

By placing my code above in the DataBindingComplete event handler (and disabling the event handler at the beginning and turning it back on at the end of the event), the columns are added at the appropriate time, and their data isn’t scrambled by .NET stupidity.

+6
source

Yes, I fought it for two days, and finally the DataBinding event fixed it.

Here is the complete code for adding a combo box to some cells in a DataGridView.

  private void LoadGrid() { DataTable dtbl = new DataTable(); dtbl.Columns.Add("FieldNo"); dtbl.Columns.Add("FieldValue"); for (int i = 0; i < 10; i++) { DataRow dr = dtbl.NewRow(); dr["FieldNo"] = i; dr["FieldValue"] = "Name " + i.ToString(); dtbl.Rows.Add(dr); } dataGridView1.DataSource = dtbl; dataGridView1.DataError += new DataGridViewDataErrorEventHandler(dtgv_ComboDataError); } private void dtgv_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) { for (int i = 0; i < dataGridView1.Rows.Count; i++) { if (i % 2 == 0) { DataGridViewComboBoxCell dCmb = new DataGridViewComboBoxCell(); dCmb.Items.Add("Yes"); dCmb.Items.Add("No"); dCmb.Value = dCmb.Items[0]; dataGridView1["FieldValue", i] = dCmb; ((DataGridViewComboBoxCell)dataGridView1["FieldValue", i]).DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox; } } } void dtgv_ComboDataError(object sender, DataGridViewDataErrorEventArgs e) { //Nothing needed here } 
+4
source

I ran into a very similar problem on a DataGridView with a DataGridViewComboBoxColumn .

DataGridView bound to a DataSet The parameters of the DataGridViewComboBoxCell drop-down list must be filled in accordance with the value of another cell of the same row.

Whether I manually fill in the elements of the ComboBox cell (the DataGridViewComboBoxCell.Items property) or use the DataBinding property ( DataGridViewComboBoxCell.DataSource ), right before entering the CellFormatting event CellFormatting values ​​are correct, but as soon as the steps in the handler are myComboBoxCell.Items.Count , myComboBoxCell.Items.Count omitted.

If the cell value is something other than DBNull.Value , this causes the unpleasant value of "System.ArgumentException: DataGridViewComboBoxCell" is invalid. "(Since myComboBoxCell.Value not contained in myComboBoxCell.Items )

My “solution” is more circumventing: I am handling the DataGridView.DataError event and populating the corresponding DataGridComboBoxCell DataSource inside this handler. Then throw the exception ( e.ThrowException = False ).

It is too dirty to my taste, but it works.

+1
source

Source: https://habr.com/ru/post/1343649/


All Articles