Click here to Skip to main content
15,886,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Updated 3-May-21 3:04am
v2

1 solution

Not sure what were you doing but following should do:
C#
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 [^]
 
Share this answer
 
Comments
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)
{
try
{
con = new MySqlConnection();
con.ConnectionString = ConfigurationSettings.AppSettings["constr"];
con.Open();
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";
con.Close();
}
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)
{
listBox2.Items.Add(l);
}

}
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)
{
listBox2.Items.Add(l);
}
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900