Click here to Skip to main content
Sign Up to vote bad
good
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 - 22:52
Edited 7 Nov '12 - 22:54
jim lahey21.5K


2 solutions

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

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 443
1 Arun Vasu 381
2 OriginalGriff 345
3 Maciej Los 191
4 Aarti Meswania 190
0 Sergey Alexandrovich Kryukov 9,755
1 OriginalGriff 7,559
2 CPallini 4,018
3 Rohan Leuva 3,362
4 Maciej Los 2,951


Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 8 Nov 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid