473,385 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Ado.net: how to assign the column value in a datatable to combobox.?

40
hi all
i want to show a column value from datatable.I tried as below . it doesnt shows the values.

pls help.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.                 SqlDataAdapter adpt = new SqlDataAdapter(cmd);
  4.                 DataTable dtCust1 = new DataTable("CustomerDatTab");
  5.                 adpt.Fill(dtCust1);
  6.  
  7.                   dataGridView1.DataSource=dtCust1; //datagrid shows data.
  8.  
  9.  
  10.                DataRow dr = new DataRow();
  11.              foreach (DataRow dr in dtCust1.Rows)
  12.                   comboBox1.Items.Add(dr.ToString());
  13.  
  14. the below code also didnt works. it tried in the below way also
  15.                    //comboBox1.DisplayMember = "CompanyName";
  16.                 //for (int i = 0; i < dtCust1.Rows.Count - 1; i++)
  17.                      //comboBox1.Items.Add(dtCust1);
  18.  
  19.  
  20.                 comboBox1.DataSource = dtCust1.DefaultView;
  21.  
  22.  
  23.  
  24.  
  25.  

thank u
Jan 4 '08 #1
2 2319
nateraaaa
663 Expert 512MB
DataRow dr = new DataRow();
foreach (DataRow dr in dtCust1.Rows)
comboBox1.Items.Add(dr.ToString());

the below code also didnt works. it tried in the below way also
//comboBox1.DisplayMember = "CompanyName";
//for (int i = 0; i < dtCust1.Rows.Count - 1; i++)
//comboBox1.Items.Add(dtCust1);
Actually both ways should work if coded correctly

Expand|Select|Wrap|Line Numbers
  1. foreach(DataRow dr in dtCust1.Rows)
  2. {
  3. combobox1.Items.Add(dr["columnname"].ToString());
  4. }
  5.  
or

Expand|Select|Wrap|Line Numbers
  1. for(int i=0; i<dtCust1.Rows.Count - 1;i++)
  2. {
  3. combobox1.Items.Add(dtCust1.Rows[i]["columnname"].ToString());
  4. }
No DataBind is necessary if you are adding items to your combobox in this manner. If you are going to use a stored procedure to add these items to your combobox then you will need to assign a datasource to the combobox and set the text and value properties of the combobox.

Nathan
Jan 4 '08 #2
kunal pawar
297 100+
'Bind the DataTable to the ComboBox
ComboBox1.DataSource = dtCust1
ComboBox1.DisplayMember = "FirstName"
ComboBox1.ValueMember = "ID"

u can directly bind combo box to the data table

refer link

http://www.java2s.com/Code/VB/Database-ADO.net/DatabaseDataBindingComboBox.htm
Jan 5 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Bill C. | last post by:
Hello, I know this has been discussed a lot already because I've been searching around for information the last few weeks. I'm trying to implement a DataGridComboBoxColumn class. I've found...
2
by: Vern | last post by:
I created a table in a dataset that contains multiple columns. I attached a combo box to that table. Now I'm doing something wrong with trying to retrieve the value of the third column of the...
2
by: Daniel | last post by:
I'm new to .Net and all of its abilities so I hope this makes sense. Basically I'm confused on when is the appropriate time to use web forms controls vs. regular HTML. For example in ASP...
4
by: Joe Schmoe | last post by:
All I want to to be able to take a two-column DataReader (One column with the Item ID number, the other with Item Description text) and load it into a Windows Forms ComboBox (Set to DropDownList...
1
by: kingster | last post by:
Hi, I have a regular dataset and all i want to do is make a pivot table display in a browser with the datasource of the pivot table to be this dataset and then the end-user will be able to do...
4
by: Matt | last post by:
I have been searching all over the web for a way to sort a DataGridView based on the actual text being shown in a ComboBox column as opposed to the underlying value (an ID in this case). Can anyone...
3
by: sklett | last post by:
I'm changing from a DataGrid to a DataGridView and have run across a problem. The items that are bound to the DataGrid have an int Property that represents a primary key of a lookup table in my...
1
Plater
by: Plater | last post by:
I feel like this question has been asked before, but I couldn't find it. I think I am missing something obvious. Basically I am going to have a DataGridView with two columns. One textbox, one...
2
by: anatanat | last post by:
hello :) I want to bind a combobox to a specific column in a DataTable. I want the combobox items to display distinct values. I tried: combo.datasource = dataTable; combo.DisplayMember =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.