Follows Following Steps to Binding ComboBox in C# :
1.Define namespace for sqlconnection <using System.Data.SqlClient;>
2.define Object for Binding Source
<private Binding Source bindingSource1 = new BindingSource();>
3.Define Adapter Object for Adopt data from Database
< private SqlDataAdapter dataAdapter = new SqlDataAdapter();>
4.Define Connection String for Connection in forms
< String connectionString = "Data Source=.\\sqlexpress;Initial Catalog=schoolDB;Integrated Security=True"; >
5.Then Adapter Call and pass command and connection that two argument pass
<dataAdapter = new SqlDataAdapter(selectCommand, connectionString);>
6.Then Sql commandbuilder call for execuate command in the database
< SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);>
7.Then DataTable Call for Store the Value in table
< DataTable table = new DataTable();>
8.Then this data to localize in local System or Space
< table.Locale = System.Globalization.CultureInfo.InvariantCulture;>
9.Then This Table Data fill by adapter
<dataAdapter.Fill(table);>
10.Then Set BindSource to Table
<bindingSource1.DataSource = table;>
11.Then Set ComboBox DisplayMember and ValueMember Property
<ComboBox1.DisplayMember = "School_name";
ComboBox1.ValueMember = "id";>
12 Then This code to call specific event use function and set DataSource pass a argument.
< this.Load += new System.EventHandler(button1_Click);
ComboBox1.DataSource = bindingSource1;
GetData("select School_name,id from School_info where id=13");
>Thus You Successfully Bind the combo Box to Data Base
Example Source code
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.SqlClient;
namespace ComboBox
{
public partial class buttionEvent : Form
{
private BindingSource bindingSource1 = new BindingSource();
private SqlDataAdapter dataAdapter = new SqlDataAdapter();
private void GetData(string selectCommand)
{
try
{
String connectionString = "Data Source=.\\sqlexpress;Initial Catalog=schoolDB;Integrated Security=True";
dataAdapter = new SqlDataAdapter(selectCommand, connectionString);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
bindingSource1.DataSource = table;
ComboBox1.DisplayMember = "School_name";
ComboBox1.ValueMember = "id";
}
catch (SqlException)
{
MessageBox.Show("To run this example, replace the value of the " +
"connectionString variable with a connection string that is " +
"valid for your system.");
}
}
public buttionEvent()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Load += new System.EventHandler(button1_Click);
this.Text = "DataGridView databinding and updating demo";
ComboBox1.DataSource = bindingSource1;
ComboBox1.Visible = true;
GetData("select School_name,id from School_info where id=13");
}
}
}