hi,
how to bind the control (best practice) comboboxes to the data in dataset.
thanks
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Hemant KumarPosted Sep 21, 2011, 1:28 AM
Jackuline SavarimuthuPosted Sep 8, 2011, 6:11 AM
The below code will bind the data to dropdown list, where ds is dataset which is loaded with data.
DropDownList1.DataSource = ds.Tables[0];
DropDownList1.DataTextField = "Column0"
DropDownList1.DataValueField = "Column1"
DropDownList1.DataBind();
Thanks and Regards,
Jackuline
Prabhu RajaPosted Sep 6, 2011, 2:31 AM
public static void SetBinding( Dataset dataSource, string displayMember, string valueMember )
{
Combobox1.DisplayMember = displayMember; //Dataset Column name (the Column contains String Values) to display text
Combobox1.ValueMember = valueMember; // Dataset Column name (the Column contains integer Values) to get value for text
Combobox1.DataSource = dataSource;
}
----------------------------------
if this post helps you, then mark it as "Correct Answer"
Vilas GitePosted Sep 4, 2011, 10:38 AM
Hi!
Read this one, may be helpful for you…
http://msdn.microsoft.com/en-us/library/ms973824.aspx
Thanks!
--------------------------------------------------------------------------
If this reply helps your post…then check "This is correct answer"
Satyapriya NayakPosted Sep 4, 2011, 2:52 AM
One method of binding to combobox i have already mentioned.Here are two other methods below:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Dynamically_bind_data_to_combobox_from_database
{
public partial class Form1 : Form
{
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
OleDbCommand com;
OleDbDataAdapter oledbda;
DataSet ds;
string str;
DataTable dt;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from student";
com = new OleDbCommand(str, con);
oledbda = new OleDbDataAdapter(com);
ds = new DataSet();
oledbda.Fill(ds, "student");
dt = ds.Tables["student"];
int i;
for (i = 0; i <= dt.Rows.Count - 1; i++)
{
comboBox1.Items.Add(dt.Rows[i].ItemArray[0]);
}
con.Close();
}
private void button2_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from student";
com = new OleDbCommand(str, con);
oledbda = new OleDbDataAdapter(com);
ds = new DataSet();
oledbda.Fill(ds, "student");
comboBox2.DataSource = ds.Tables["student"];
comboBox2.DisplayMember = "sname";
con.Close();
}
}
}
Thanks
If this post helps you mark it as answer
Mayur DighePosted Sep 4, 2011, 2:39 AM
if you have atleast (e.g.)2 ComboBox belonging from 2 different tables you can do it by adding following code on form_load event hanlder or on any event hanlder.
C# using SqlDataSoure:
SqlConnection Con=new SqlConnection("ConnectionString");
DataSet Dset=new DataSet();
SqlDataAdaptor adapt=new SqlDataAdaptor("Select * from Student",Con);
adapt.Fill(Dset,"Student");
SqlDataAdaptor adapt=new SqlDataAdaptor("Select * from Faculty",Con);
adapt.Fill(Dset,"Faculty");
comboBox1.DataSource=Dset.Tables["Student"].Columns["StudentName"];
comboBox2.DataSource=Dset.Tables["Student"].Columns["FacultyName"];
C# using XML:
DataSet Dset=new DataSet();
Dset.ReadXML("..\\..\\XmlDataFile.xml");
comboBox1.DataSource=Dset.Tables["Student"].Columns["StudentName"];
comboBox2.DataSource=Dset.Tables["Student"].Columns["FacultyName"];
Satyapriya NayakPosted Sep 1, 2011, 3:09 AM
Here is your desired code.Run the attachments.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace filter_dropdown
{
public partial class Form1 : Form
{
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
OleDbCommand com;
OleDbDataAdapter oledbda;
DataSet ds;
string str;
DataTable dt;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from student";
com = new OleDbCommand(str, con);
OleDbDataReader reader = com.ExecuteReader();
while (reader.Read())
{
comboBox1.Items.Add(reader["sname"]);
}
reader.Close();
con.Close();
}
}
}
Thanks
If this post helps you mark it as answer