Add an item to combobox before bind data from data base


Combobox in windows form which retrieve data from data base  but you want to add first item  like ---Select --- before the data from data base then this article is helpful to you.


code:

   
        private void Form1_Load(object sender, EventArgs e)
        {
            // Variable Delaration
            SqlConnection cnn = new SqlConnection();
            SqlCommand cmd = new SqlCommand();
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();

            // connection open
            cnn.ConnectionString=@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True";
            cnn.Open();

            // get data in datatable
            cmd.Connection = cnn;
            cmd.CommandText = "Select * from Tbl_Department";
            da.SelectCommand = cmd;
            da.Fill(ds, "Tbl_Department");
            dt = ds.Tables["Tbl_Department"];


            // assign dumy data
            DataRow dr = dt.NewRow();
            dr["Dept_Id"] = -1;
            dr["Dept_Name"] = "---Select Department---";
            dt.Rows.InsertAt(dr, 0);

            // fill data in combobox
            comboBox1.DataSource = dt;
            comboBox1.DisplayMember = "Dept_Name";
            comboBox1.ValueMember = "Dept_Id";
        }