How to bind ComboBox in window form asp.net in C#
In this article we will explain How to bind ComboBox in window form asp.net in C#. ComboBox is a control of window form application. It is like drop-down list or list box. It is used to directly select value form the ComboBox.
Step 1 : Drag and drop ComboBox on Form
Step 2 : Add Namespace
drdsrcode = dtdsrcode.NewRow();
comboBox1.ValueMember = "DSR_Code";
comboBox1.DisplayMember = "DSR_Code";
con.Close();
Step 1 : Drag and drop ComboBox on Form
Step 2 : Add Namespace
- using System.Data;
- using System.Data.SqlClient;
- using System.Configuration;
Step 3 : Create a connection by using ConfigurationManager
Example
string cs = ConfigurationManager.ConnectionStrings["sqlcon"].ConnectionString;
Step 4 : Create Method namely "dsrcodebind();"
public SubDSR()
{
InitializeComponent();
dsrcodebind();
}
Step 5 : Write the following code in this Method.
private void dsrcodebind()
{
DataRow drdsrcode;
SqlConnection con=new SqlConnection (cs);
SqlCommand com = new SqlCommand("select * from
DSR_Master1", con);
con.Open();
SqlDataAdapter sdadsrcode = new SqlDataAdapter(com);
DataTable dtdsrcode = new DataTable();
sdadsrcode.Fill(dtdsrcode);
drdsrcode.ItemArray = new object[] { 0, "--Select DSR Code--" };
dtdsrcode.Rows.InsertAt(drdsrcode,
0);
comboBox1.DataSource = dtdsrcode;
}
The output :
0 comments:
Post a Comment
Do not enter spam link