Fill Combobox In c#


1)Create Class File and Give Name "Funcation"

2) Past Code in class file

        public static void comboFill(ComboBox cmb, string strSQL, string dTable, string dDisplay, string dValue)
        {
            try
            {
                cmb.DropDownStyle = ComboBoxStyle.DropDownList;
                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(strSQL, datastore.Connection());
                da.Fill(ds, dTable);
                cmb.DisplayMember = dDisplay;
                cmb.ValueMember = dValue;
                cmb.DataSource = ds.Tables[dTable].DefaultView;
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
                return;
            }
            finally
            {
                SqlDataAdapter da = new SqlDataAdapter();
                da.Dispose();
                datastore.CloseConnection();
            }
        }
   private void button2_Click(object sender, EventArgs e)
        {
            function.comboFill(cmbBrand, "SELECT BrandId,BrandName FROM BrandMaster WHERE Active='True' order by BrandName asc ", "BrandMaster", "BrandName", "BrandId");
        }


0 comments:

Post a Comment