Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Monday, December 3, 2012 7:12 AM
Hi All,
Am not able to set the default value for the datagridView combo box column based on the Value member. in datagrid view Column zero is set as the datagridview combobox column.
Column1.DataSource = dt;
Column1.DisplayMember = "Name";
Column1.ValueMember = "ID";
"dt" table contains the following rows
ID Name
1 xxx
2 yyy
3 zzz
I want to set the default value based on the Value member. I need to set the "zzz" as default value when page loading.But in datagrid view i can able to set based on the Displaymember (i,e Name) only(see code below)
row.Cells["Column1"].Value = "zzz";
Not able to set the value based on Value member(i,e "ID").
I need to set the default value based on the Value Member. I need to set the ID as 3 so that it will display the "zzz" as display string in the screen. is it possible? If so, please drive me through code.
Manikandan Murugeshan
All replies (3)
Sunday, December 9, 2012 2:14 PM âś…Answered
Hi Manikandan,
If you want to set the default value by the value member you can try below sample.
DataTable dt = new DataTable(); DataColumn dc1 = new DataColumn("ID"); DataColumn dc2 = new DataColumn("Name"); dt.Columns.Add(dc1); dt.Columns.Add(dc2); dt.Rows.Add(1, "name1"); dt.Rows.Add(2, "name2"); DataGridViewComboBoxColumn c1 = new DataGridViewComboBoxColumn(); c1.DataSource = dt; c1.DisplayMember = "Name"; c1.ValueMember = "ID"; for (int i = 0; i < dt.Rows.Count; i++) { if (dt.Rows[i]["ID"].ToString() == "2")//set the default value based on value member ID { c1.DefaultCellStyle.NullValue = dt.Rows[i]["Name"]; } } dataGridView1.Columns.Add(c1);
Good day.
Bob Shen
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Monday, December 3, 2012 8:14 AM
in edit column window bind the DataPropertyName property to your target field (ID)
Monday, December 3, 2012 9:42 AM
Hi Pooyan Fekrati,
I tried in that way also. But failed. It throws exception that is invalid. See what i made in the code.
Column3.DataSource = dt;
Column3.DisplayMember = "Name";
Column3.ValueMember = "ID";
Column3.DataPropertyName = "ID";
and I set the default value like
row.Cells["Column1"].Value = 3;
Is this correct?
Manikandan Murugeshan