Click here to Skip to main content
16,017,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a datagridviewcomboboxcolumn in datagridview and I want to display Item2 as selected in datagridviewcomboboxcolumn by default when form loads/displayed.

I have 3 items in my database table Records

id Name
1 Item1
2 Item2
3 Item3

If somebody have any idea please do help me.
Thanks in advance.
Posted

you can use cell formatting event like in the below case :-
DataGridViewComboBoxColumn cmbCol;
private void Form7_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("c1",typeof(int));
dt.Columns.Add("c2");
for (int j = 0; j < 5; j++)
{
dt.Rows.Add(j,"c"+j.ToString());
}
DataTable dt2 = new DataTable();
dt2.Columns.Add("c1", typeof(int));
dt2.Columns.Add("c2");
for (int j = 5; j < 10; j++)
{
dt2.Rows.Add(j, "c" + j.ToString());
}
this.dataGridView1.DataSource = dt;
cmbCol = new DataGridViewComboBoxColumn();
cmbCol.DisplayMember = "c2";
cmbCol.ValueMember = "c1";
//cmb.DataPropertyName = "c1";
cmbCol.DataSource = dt2;
this.dataGridView1.Columns.Add(cmbCol);
this.dataGridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);
}
void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == 2)
{
DataTable dt = this.cmbCol.DataSource as DataTable;
e.Value = dt.Rows[0]["c2"];
}
}
}
 
Share this answer
 
Comments
agent_kruger 14-Nov-13 23:58pm    
or by setting selected value of combobox
Hi you may use CurrentCell Properties

C#
dataGrid.CurrentCell = dataGrid[columnIndex, rowIndex];


Hope it helps
 
Share this answer
 
v2
Comments
MeJainManish 6-Aug-15 6:49am    
currentcell property will focus the cell of the datagridview only, but according to question he need the selection of item from the datagridviewcombobox column

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900