Devexpress 使用comboBoxEdit 和LookUpEditor 控件数据绑定

Devexpress 中,使用comboBoxEdit 和 LookUpEditor 控件进行数据绑定:

  1 #region 下拉框 comboBoxEdit 和 LookUpEditor  

  2   

  3 //  设置下拉框   传入参数(需要设置的下拉框控件,查询的语句,默认第一行的内容)  

  4 public static void SetComboList(DevExpress.XtraEditors.ComboBoxEdit ComboList, string QuerySQL, string FirstRowText = "",bool bSelectFirstItem=false)  

  5 {  

  6     DataTable dtList=null;  

  7     try  

  8     {  

  9         dtList = sqlHelper.ExecuteDataTable(Common.ConnString, CommandType.Text, QuerySQL, null);  

 10     }  

 11     catch (Exception ex)  

 12     {  

 13         frm0201.DisplayMsg("数据访问异常:", ex.Message.ToString());  

 14         return;  

 15     }  

 16   

 17     int intCount = (dtList!=null) ? dtList.Rows.Count : 0;  

 18     ComboList.Properties.TextEditStyle = TextEditStyles.DisableTextEditor; // 设置 comboBox的文本值不能被编辑  

 19     ComboList.Properties.Items.Clear();  

 20     if (!string.IsNullOrEmpty(FirstRowText)) ComboList.Properties.Items.Add(FirstRowText);  

 21   

 22     if (intCount > 0)  

 23     {  

 24         for (int i = 0; i < intCount; i++)  

 25         {  

 26             ComboList.Properties.Items.Add(dtList.Rows[i][0].ToString());  

 27         }  

 28     }  

 29     if (bSelectFirstItem) ComboList.SelectedIndex = 0; // 设置选中第1项  

 30 }  

 31   

 32 //设置下拉框   loe 为下拉框名, sSql 为下拉框的数据来源  

 33 public static void SetLookUpList(ref DevExpress.XtraEditors.LookUpEdit loeSource, string strSQL, int ValueCol = 0, int NameCol = 1,DataTable dtSource=null,bool bSelectFirstItem=false,string sFirstRowText="")  

 34 {  

 35     DataTable dt =null;  

 36   

 37     try  

 38     {  

 39         if (!string.IsNullOrEmpty(strSQL))  

 40         {  

 41             dt = sqlHelper.ExecuteDataTable(Common.ConnString, CommandType.Text, strSQL, null);  

 42         }  

 43         else if (dtSource != null)  

 44         {  

 45             dt = dtSource.Copy();  

 46         }  

 47     }  

 48     catch (Exception ex)  

 49     {  

 50         frm0201.DisplayMsg("数据访问异常:", ex.Message.ToString());  

 51         return;  

 52     }  

 53   

 54     loeSource.Properties.Columns.Clear();  

 55     loeSource.Properties.DataSource = null;  

 56   

 57     if (dt!=null && dt.Rows.Count > 0 && dt.Columns.Count > 1) // 一般为2列: id列 和 value 列  

 58     {  

 59         loeSource.Properties.Columns.Add(new LookUpColumnInfo(dt.Columns[ValueCol].ColumnName.ToString()));  

 60         loeSource.Properties.Columns[ValueCol].Visible = false; // 隐藏id列  

 61         loeSource.Properties.Columns.Add(new LookUpColumnInfo(dt.Columns[NameCol].ColumnName.ToString()));  

 62   

 63         if (!string.IsNullOrEmpty(sFirstRowText))  

 64         {  

 65             DataRow dr = dt.NewRow();  

 66             dr[dt.Columns[ValueCol]] = -1; // 这一项的 ID 值为 -1  

 67             dr[dt.Columns[NameCol]] = sFirstRowText;  

 68             dt.Rows.InsertAt(dr, 0);// 从索引为0的位置插入  

 69         }  

 70   

 71         loeSource.Properties.DataSource = dt;  

 72         loeSource.Properties.ValueMember = dt.Columns[ValueCol].ColumnName; // 对应ID  

 73         loeSource.Properties.DisplayMember = dt.Columns[NameCol].ColumnName; // 显示内容  

 74   

 75         if (bSelectFirstItem) // 是否选中第一项  

 76         {  

 77             int iID=0;  

 78             if(Int32.TryParse(dt.Rows[0][0].ToString(),out iID))  

 79             {  

 80                 loeSource.EditValue = iID;  

 81             }  

 82         }  

 83     }  

 84   

 85     loeSource.Properties.ShowHeader = false;  

 86     loeSource.Properties.ShowFooter = false;  

 87     loeSource.Properties.NullText = "";  

 88 }  

 89   

 90 // 得到下拉框 的内容项,以Keys.Tab拼成一个字符串   

 91 public static string GetComboList(string sQuerySQL)  

 92 {  

 93     try  

 94     {  

 95         DataTable dtList = sqlHelper.ExecuteDataTable(Common.ConnString, CommandType.Text, sQuerySQL, null);  

 96         if (dtList!=null && dtList.Rows.Count > 0 && dtList.Columns.Count == 1) // 有数据且只有一列  

 97         {  

 98             string sResult = string.Empty;  

 99             for (int i = 0; i < dtList.Rows.Count; i++)  

100             {  

101                 sResult += dtList.Rows[i][0].ToString() + Convert.ToChar(Keys.Tab);  

102             }  

103   

104             if (sResult.Length > 1)  

105             {  

106                 sResult = sResult.Remove(sResult.Length - 1);  

107             }  

108             return sResult;  

109         }  

110     }  

111     catch (Exception ex)  

112     {  

113         XtraMessageBox.Show(ex.Message.ToString());  

114     }  

115   

116     return "";  

117 }  

118  

119 #endregion  

 

你可能感兴趣的:(DevExpress)