Click here to Skip to main content
11,922,416 members (29,797 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#
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
Edited 7-Nov-12 23:54pm
jim lahey24.9K
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";
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.

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

  Print Answers RSS
0 OriginalGriff 453
1 Dave Kreskowiak 325
2 Sergey Alexandrovich Kryukov 273
3 Richard MacCutchan 235
4 ppolymorphe 175
0 OriginalGriff 7,231
1 KrunalRohit 4,530
2 Sergey Alexandrovich Kryukov 3,356
3 George Jonsson 2,865
4 Suvendu Shekhar Giri 2,216

Advertise | Privacy | Mobile
Web03 | 2.8.151120.1 | Last Updated 8 Nov 2012
Copyright © CodeProject, 1999-2015
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