The main purpose of "Dot Net Labs" provide the dot net program. You can learn dot net, .Net Core, C#, SQL, Linq step-by-step.

Tuesday 17 January 2017

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 
  •       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 = dtdsrcode.NewRow();
            drdsrcode.ItemArray = new object[] { 0, "--Select DSR Code--" };
            dtdsrcode.Rows.InsertAt(drdsrcode, 0);
            comboBox1.ValueMember = "DSR_Code";
            comboBox1.DisplayMember = "DSR_Code";
            comboBox1.DataSource = dtdsrcode;
            con.Close();
        }

The output :

How-to-bind-ComboBox-in-window-form-asp-net-in-C-sharp

0 comments:

Post a Comment

Do not enter spam link

Popular Posts