Why the DataGridViewComboBoxColumn values are lost?!!!

May 8 2006 2:03 PM

Hi everybody!

 

I have DataGridView whose sourse I set to a datatable with 4 columns

containing data. I then remove two columns, create a DataGridViewComboBoxColumn and DataGridViewCheckBoxColumn and insert this into the same location as the columns that were removed. I want to set as the selected value in the combo box of every cell in the DataGridView a specific value. I have done it, but once the debugger leaves the file with the code that executes this functionality, the values that were set in the combo box and check box are lost.

 

I don't know whether the problem is because I removed and then added these columns. At the end of the Load() method I have this line: dataGridView.AutoGenerateColumns = false; if I don't use it, the column that I removed appears again. Any idea about why the value of the DataGridComboBoxColumn is changed to null automatically and how to solve it?

 

Other thing is that if I change the values of the combo box in the datagridview and later I want to save them, when I get the values are not the ones I chose, but the values that I set at the beginning as the datasource of the datagridview.

 

This is part of the code I’m using:

 

private void FrmTestCases_Load(object sender, EventArgs e)

{

DataTable dt = new DataTable();

 

dt.Columns.Add(new DataColumn("Path", typeof(string)));

dt.Columns.Add(new DataColumn("Category", typeof(string)));

dt.Columns.Add(new DataColumn("Include", typeof(string)));

dt.Columns.Add(new DataColumn("Description", typeof(string)));

 

dataGridView.DataSource = dt;

 

dataGridView.Columns.Remove("Category");

dataGridView.Columns.Remove("Include");

DataGridViewComboBoxColumn cb = new DataGridViewComboBoxColumn();

DataGridViewCheckBoxColumn kb = new DataGridViewCheckBoxColumn();

cb = CreateComboBoxColumn("Category", "Category");           

kb = CreateCheckBoxColumn("Include", "Include");

 

dataGridView.Columns.Insert(1, cb);

dataGridView.Columns.Insert(2, kb);

}

 

// ---------

private void OnFormClosing(object sender, FormClosingEventArgs e)

{

DataRowView testCase = (DataRowView)dataGridView.Rows[0].DataBoundItem;

String fileStream = testCase["Include"] + "," + testCase["Category"];

}

 

I've been working on it for days but NO LUCK !!  Can you please give me a hand?!

 

Elvia