将SelectedIndex设置为DataGridViewComboBoxColumn [英] set SelectedIndex to DataGridViewComboBoxColumn

查看:76
本文介绍了将SelectedIndex设置为DataGridViewComboBoxColumn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

这是我的代码

我确实读取了数据库值,但如何在此处设置selectedindex ???????????????

hi every body

this is my code

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

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..............

推荐答案

不需要迭代DataTable的所有行,为它设置SelectedIndex. ComboBoxColumn.而是按以下示例中给出的示例所示设置DataGridViewComboBoxColumn的DataSource,DisplayMember和ValueMember属性,
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 ...


这篇关于将SelectedIndex设置为DataGridViewComboBoxColumn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆