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 22:52pm
Edited 7-Nov-12 22:54pm
jim lahey24.9K
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.
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

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

  Print Answers RSS
0 OriginalGriff 225
1 Richard MacCutchan 100
2 kbrandwijk 90
3 ChauhanAjay 80
4 ProgramFOX 80
0 Sergey Alexandrovich Kryukov 9,050
1 OriginalGriff 8,151
2 CPallini 2,613
3 Richard MacCutchan 2,221
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web02 | 2.8.140827.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