ublic static void SetComboList(DevExpress.XtraEditors.ComboBoxEdit ComboList, string QuerySQL, ArrayList arrSource = null, DataTable dtSource = null, string FirstRowText = "", bool bSelectFirstItem = false, bool bEditable = false)
{
DataTable dtList = null;
if (dtSource != null)
{
dtList = dtSource;
}
else if (arrSource != null)
{
dtList = new DataTable();
dtList.Columns.Add("name");
foreach (string s in arrSource)
{
dtList.Rows.Add(s);
}
}
else if (!string.IsNullOrEmpty(QuerySQL))
{
try
{
dtList = SqlHelper.ExecuteDataTable(SqlHelper.ConnString, CommandType.Text, QuerySQL, null);
}
catch (Exception ex)
{
Common.DisplayMsg("數據訪問異常:", ex.Message.ToString());
return;
}
}
else { return; }
int intCount = (dtList != null) ? dtList.Rows.Count : 0;
if (bEditable)
{
ComboList.Properties.TextEditStyle = TextEditStyles.Standard;
}
else
{
ComboList.Properties.TextEditStyle = TextEditStyles.DisableTextEditor; // 設置comboBox的文本值不能被編輯
}
ComboList.Properties.Items.Clear();
if (!string.IsNullOrEmpty(FirstRowText)) ComboList.Properties.Items.Add(FirstRowText);
if (intCount > 0)
{
for (int i = 0; i < intCount; i++)
{
ComboList.Properties.Items.Add(dtList.Rows[i][0].ToString());
}
}
if (bSelectFirstItem) ComboList.SelectedIndex = 0; // 設置選中第1項
}
摘自:keenweiwei的專欄