当前位置:编程学习 > C#/ASP.NET >>

winform: 设置下拉框数据源的公共函数

 

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的专栏

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,