Click here to Skip to main content
15,881,881 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 :

C#
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;
           }
       }

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

pls help me
Posted
Updated 7-Nov-12 22:54pm
v2

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.
 
Share this answer
 
Comments
f.sarikhani 8-Nov-12 5:10am    
thanks very much
jim lahey 8-Nov-12 5:18am    
My pleasure. If it helped you can mark it as the answer or rate it.
C#
(dataGridView1.Columns[4] as DataGridViewComboBoxColumn).DataSource = p;
dataGridView1.Columns[4].DisplayMember ="Name";
dataGridView1.Columns[4].ValueMember = "Id";
 
Share this answer
 
v2

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