从表的列读取数据到组合框 [英] read data from a column of table to a combobox

查看:77
本文介绍了从表的列读取数据到组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个组合框的数据源设置为表的一列,而此组合框是gridview列之一,我的代码是:

i want to set data source of one combo box to one column of table and this combo box is one of gridview column my code is :

DataTable p = new System.Data.DataTable();
            p = selectcolor();
            (dataGridView1.Columns[4] as DataGridViewComboBoxColumn).DataSource = p;
 
 

private DataTable selectcolor()
       {
 
           DataTable k = new System.Data.DataTable();
           try
           {
 
               string str = "Data Source=C:\\Documents and Settings\\almas\\Desktop\\BazarKhodro\\khodro.sdf";
               Qconnection.ConnectionString = str;
               Qcommand.Connection = Qconnection;
 

               string commandText = "select color from foroosh";
 
               Qcommand.CommandText = commandText;
               Qcommand.CommandType = CommandType.Text;
               SqlCeDataAdapter a = new SqlCeDataAdapter();
               a.SelectCommand = Qcommand;
               a.Fill(k);
               Qconnection.Open();
               Qconnection.Close();
               return k;
 
           }
           catch (Exception ex)
           {
 
               throw new Exception(ex.Message);
               return k;
           }
       }


运行后,我看到了p的内容,但是combobox的内容是:
System.Data.DataRowView

请帮助我


after runing i see the content of p but content of combobox is:
System.Data.DataRowView

pls help me

推荐答案

您需要设置DataGridViewComboBoxColumn:
的DisplayMember和ValueMember属性.
http://msdn.microsoft.com/en-us/library/system. windows.forms.datagridviewcomboboxcolumn.aspx [ ^ ]

只有这样,才能在您的组合框中显示正确的数据.
You need to set the DisplayMember and ValueMember properties of the DataGridViewComboBoxColumn:

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcolumn.aspx[^]

Only that way will the correct data be displayed in your combo box.


(dataGridView1.Columns[4] as DataGridViewComboBoxColumn).DataSource = p;
dataGridView1.Columns[4].DisplayMember ="Name";
dataGridView1.Columns[4].ValueMember = "Id";


这篇关于从表的列读取数据到组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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