Click here to Skip to main content
Rate this: bad
good
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();
           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 7-Nov-12 23:52pm
Edited 7-Nov-12 23:54pm
jim lahey24.9K
v2
Rate this: bad
good
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";
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
f.sarikhani at 8-Nov-12 5:10am
   
thanks very much
jim lahey at 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 Maciej Los 310
1 OriginalGriff 285
2 Afzaal Ahmad Zeeshan 200
3 Sergey Alexandrovich Kryukov 195
4 BillWoodruff 180
0 OriginalGriff 6,499
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,193
3 Manas Bhardwaj 4,657
4 Maciej Los 4,120


Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 8 Nov 2012
Copyright © CodeProject, 1999-2014
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