Click here to Skip to main content
11,926,961 members (49,758 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#3.0 C#
how to bind a column of database to list box control in windows form application............and the main point is that, that prevent to System.Data.DataRowView Error when copying all items of one list box to another list box...
Posted 24-Jan-13 3:41am
Edited 24-Jan-13 3:44am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Not sure what were you doing but following should do:
private void BindComboBox()
  comboBox1.DataSource = dataSet1.Tables["Suppliers"];
  comboBox1.DisplayMember = "ProductName";
Details here: MSDN: How to: Bind a Windows Forms ComboBox or ListBox Control to Data [^]
Shubh Agrahari 25-Jan-13 2:49am
Sandeep Sir i have to do copy all items from one list box to another list box where as the 1st list box bind from a data source(database Column). there is a button like >> on btn's click event i tried many type of code like listbox2.items.addrange(listbox1.items); and make a foreach loop condition..........but still when run the project and click the >> btn then in 2nd list showing System.Data.DataRowView for each items.....i tried my top in ma project as well as on google there i got many solutions but they are not gives proper result on my condition....where is fault in binding database or in retrieving copy methodology .....
Sandeep Mewara 25-Jan-13 2:56am
It would be better if you improve question with your related code snippet. Posting incomplete question does not help most of the times.
Shubh Agrahari 25-Jan-13 4:35am
ok sir...there is my code that run on radio button checked event....

if (radioButton2.Checked == true)
con = new MySqlConnection();
con.ConnectionString = ConfigurationSettings.AppSettings["constr"];
string str = "select CONCAT(dname, ' (' ,relation,')') AS mixname from empdependents where pfno='" + sd.epfno + "' and (relation='wife' or relation='daughter' or relation='son' or relation='husband')";
da = new MySqlDataAdapter(str, con);
ds = new DataSet();
da.TableMappings.Add("table", "empdependentd");
da.Fill(ds, "empdependents");
this.listBox1.DataSource = this.ds.Tables["empdependentd"];
this.listBox1.DisplayMember = "empdependents.mixname ";
this.listBox1.ValueMember = "empdependents.mixname";
catch (Exception ex)
MessageBox.Show("Error" + ex.Message);

and after it there is code by this i want to retrieve all items of 1st list box to another list box...
private void button6_Click(object sender, EventArgs e)
//listBox2.Items.AddRange(listBox1.Items);// 1st i tried this

foreach (object l in listBox1.Items)

but still showing 2nd list box that System.Data.DateRowView..
Sandeep Mewara 25-Jan-13 9:31am
First of all, make sure listbox2 has the same settings as listbox1, e.g. listbox2.ValueMember, listbox2.DisplayMember.

Try this:
foreach (var l in listBox1.Items)
Shubh Agrahari 29-Jan-13 9:04am
ist i tried var but when look that still problam is stand then i use object...but thank sir it was some system error that was solved by restarting now running proper

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web04 | 2.8.151126.1 | Last Updated 24 Jan 2013
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