Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi every body

this is my code

i did read database values but how to set selectedindex here???????????????

C#
dataGridView2.DataSource = List;

for (int i = 0; i < dataGridView2.Rows.Count; i++)
                {
                    DataGridViewComboBoxColumn column = (DataGridViewComboBoxColumn)this.dataGridView2.Columns[0];
                    Typess = Type;
                    switch (Typess)
                    {
                        case _blank_:
                            //how to set selectedinedex here......
                            //column .selectedindex=0;
                            break;
                        case Order:
                             //how to set selectedinedex here......
                            //column .selectedindex=1;
                            break;
                        case Sales_Shipment:
                             //how to set selectedinedex here...... 
                            //column .selectedindex=2;
                            break;
                        default:
                            break;
                    }
                }


Thanks in advance..............
Posted
Updated 5-Jun-12 23:15pm
v3

1 solution

It is not required to iterate all rows of the DataTable set the SelectedIndex for the ComboBoxColumn. Instead the DataSource, DisplayMember and ValueMember properties of DataGridViewComboBoxColumn as to be set as shown in the example given here http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcolumn.datasource.aspx[^]

The DataGridView displays the DisplayMember field of the DataSource of ComboBoxColumn corresponding to the ValueMember.

For eg. the DataSource of the ComboBox column is set to a DataTable like
Sl.No. Title
1 Mr
2 Mrs
3 Ms

Now, the DataTable for which DataGridView contains a filed Title with following values


.... 1 ...
.... 3 ...
.... 2 ...

When the DataGridComboBoxColumn is setup with ValueMember as SlNo and DisplayMember as Title then it displays like below
.... Mr ...
.... Ms ...
.... Mrs ...
 
Share this answer
 

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