Click here to Skip to main content
13,290,735 members (79,145 online)
Rate this:
Please Sign up or sign in to vote.
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 :

DataTable p = new System.Data.DataTable();
            p = selectcolor();
            (dataGridView1.Columns[4] as DataGridViewComboBoxColumn).DataSource = p;
private DataTable selectcolor()
           DataTable k = new System.Data.DataTable();
               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;
               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:

pls help me
Posted 7-Nov-12 23:52pm
Updated 7-Nov-12 23:54pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You need to set the DisplayMember and ValueMember properties of the DataGridViewComboBoxColumn:[^]

Only that way will the correct data be displayed in your combo box.
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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.171207.1 | Last Updated 8 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100