ValueMember and DisplayMember on DataGridViewComboBoxCell
-
I'm hoping someone can help because I'm stumped here. I have a
DataGridView
that is populated with a SQL query. For simplicity's sake, let's say the SQL query is as follows:SELECT Name, Number, Lookup FROM ValuesTable
Now, depending on the value of the 'Lookup' column, the value in the 'Number' column can either be a straight forward value (when Lookup==0) or a reference to a different table (when Lookup==1), in which case I want a
ComboBox
in the cell which shows the various options in that other table. In other words, I can't really useDataGridViewComboBoxColumn
because not all cells in the column areComboBoxes
, only those for which the Lookup value is 1. So far, so good. In theDataBindingComplete
event handler of theDataGridView
I have code similar to the following to put aComboBox
(and populate it) into the 'Number' cell of each row that has a value of 1 in the 'Lookup' cell.foreach (DataGridViewRow row in myDataGridView.Rows)
{
if (row.Cells["Lookup"].Value.ToString() != "0")
{
DataGridViewComboBoxCell myComboCell = new DataGridViewComboBoxCell();
using (SqlCommand mySqlCommand = new SqlCommand("SELECT Number, Text FROM LookupTable"))
{
using (SqlDataReader mySqlDataReader = sqlCommand.ExecuteReader())
{
DataSet ds1 = new DataSet();
DataTable dt1 = new DataTable();
ds1.Tables.Add(dt1);
ds1.Load(mySqlDataReader, LoadOption.PreserveChanges, ds1.Tables[0]);
myComboCell.DataSource = ds1.Tables[0];
myComboCell.DisplayMember = "Text";
myComboCell.ValueMember = "Number";
}
}
row.Cells["Value"] = newCell;
}
}This sort of works, but not quite. All the appropriate cells in the
DataGridView
has aComboBox
inside them and the options of theComboBox
are those retrieved from the Lookup Table. But when an option is selected in aComboBox
and the focus is moved away from that cell, the actual value displayed in the cell is the number by which the value was looked up. Also, if theDataGridView
is populated (like described above), all the cells withComboBoxes
show the number that is stored in the 'Number' field of ValuesTable, not the 'Text' field of LookupTable likemyComboCell.DisplayMember = "Text"
dictates. I'd really appreciate if someone co -
I'm hoping someone can help because I'm stumped here. I have a
DataGridView
that is populated with a SQL query. For simplicity's sake, let's say the SQL query is as follows:SELECT Name, Number, Lookup FROM ValuesTable
Now, depending on the value of the 'Lookup' column, the value in the 'Number' column can either be a straight forward value (when Lookup==0) or a reference to a different table (when Lookup==1), in which case I want a
ComboBox
in the cell which shows the various options in that other table. In other words, I can't really useDataGridViewComboBoxColumn
because not all cells in the column areComboBoxes
, only those for which the Lookup value is 1. So far, so good. In theDataBindingComplete
event handler of theDataGridView
I have code similar to the following to put aComboBox
(and populate it) into the 'Number' cell of each row that has a value of 1 in the 'Lookup' cell.foreach (DataGridViewRow row in myDataGridView.Rows)
{
if (row.Cells["Lookup"].Value.ToString() != "0")
{
DataGridViewComboBoxCell myComboCell = new DataGridViewComboBoxCell();
using (SqlCommand mySqlCommand = new SqlCommand("SELECT Number, Text FROM LookupTable"))
{
using (SqlDataReader mySqlDataReader = sqlCommand.ExecuteReader())
{
DataSet ds1 = new DataSet();
DataTable dt1 = new DataTable();
ds1.Tables.Add(dt1);
ds1.Load(mySqlDataReader, LoadOption.PreserveChanges, ds1.Tables[0]);
myComboCell.DataSource = ds1.Tables[0];
myComboCell.DisplayMember = "Text";
myComboCell.ValueMember = "Number";
}
}
row.Cells["Value"] = newCell;
}
}This sort of works, but not quite. All the appropriate cells in the
DataGridView
has aComboBox
inside them and the options of theComboBox
are those retrieved from the Lookup Table. But when an option is selected in aComboBox
and the focus is moved away from that cell, the actual value displayed in the cell is the number by which the value was looked up. Also, if theDataGridView
is populated (like described above), all the cells withComboBoxes
show the number that is stored in the 'Number' field of ValuesTable, not the 'Text' field of LookupTable likemyComboCell.DisplayMember = "Text"
dictates. I'd really appreciate if someone codear dewald try this order display member value member datasource i hope it help you