winform下自带的Combox 绑定数据源 样例

   public static void SetComboList(ComboBox cboSource, string sSQL,string sWindowTxt,string sFirstItem="",int iFirstItemValue=-1,DataTable dtSource=null)

       {

           string sError = string.Empty;

           DataTable dt=null;



           if (!string.IsNullOrEmpty(sSQL))

           {

               dt = SqlLiteHelper.GetDataTable(out sError, sSQL);

               if (!string.IsNullOrEmpty(sError))

                   Common.DisplayMsg(sWindowTxt, sError);

           }

           else if (dtSource != null)

           {

               dt = dtSource.Copy();

           }



           if (!string.IsNullOrEmpty(sFirstItem))

           {

               DataRow dr = dt.NewRow();

               dr[0] = iFirstItemValue;

               dr[1] = sFirstItem;



               dt.Rows.InsertAt(dr, 0);

           }



           //cboSource.Items.Clear();



           cboSource.DataSource = dt;

           cboSource.ValueMember = dt.Columns[0].ColumnName;

           cboSource.DisplayMember = dt.Columns[1].ColumnName;

           cboSource.DropDownStyle = ComboBoxStyle.DropDownList;

       }


调用方法:

        private void InitOutputType()
        {
            string sSql = "select * from tgExpenditureItem";
            Common.SetComboList(cboOutputItem, sSql, this.Text);
        }

 

你可能感兴趣的:(WinForm)