控件帮助类

技术文章 1年前 (2020) 完美者
759 0

标签:datarow   node   static   mem   tab   combobox   判断   isp   checked   

    public class ControlHelper
    {
        /// <summary>
        /// ComboBox控件绑定数据集列
        /// </summary>
        /// <param name="cb">控件名称</param>
        /// <param name="dt">数据集</param>
        /// <param name="columnName">列名</param>
        public static void BindComboBox(ComboBox cb,DataTable dt,string columnName)
        {
            foreach(DataRow row in dt.Rows)
            {
                cb.Items.Add(row[columnName].ToString());
            }
        }
        /// <summary>
        /// ComboBox绑定数据,并排序
        /// </summary>
        /// <param name="cb">控件名称</param>
        /// <param name="dt">数据集</param>
        /// <param name="columnName">列名</param>
        /// <param name="isSorted">是否排序</param>
        public static void BindComboBox(ComboBox cb,DataTable dt,string columnName,bool isSorted)
        {
            if (!isSorted)
            {
                BindComboBox(cb, dt, columnName);
            }
            else
            {
                DataView view = dt.DefaultView;
                view.Sort = string.Format("{0}", columnName);
                for(int i = 0; i < view.Count; i++)
                {
                    cb.Items.Add(view[i][columnName].ToString());
                }
            }
        }
        /// <summary>
        /// ComboBox绑定与显示
        /// </summary>
        /// <param name="cb">控件</param>
        /// <param name="dt">数据集</param>
        /// <param name="displayMember">显示名称</param>
        /// <param name="valueMember">存储名称</param>
        public static void BindComboBox(ComboBox cb,DataTable dt,string displayMember,string valueMember)
        {
            cb.DisplayMember = displayMember;
            cb.ValueMember = valueMember;
            cb.DataSource = dt;
        }
        /// <summary>
        /// TreeView控件绑定数据集
        /// </summary>
        /// <param name="treeView">控件名称</param>
        /// <param name="dt">数据集</param>
        /// <param name="columnName">列名</param>
        public static void BindTreeView(TreeView treeView,DataTable dt,string columnName)
        {
            foreach(DataRow row in dt.Rows)
            {
                treeView.Nodes.Add(row[columnName].ToString());
            }
        }
        /// <summary>
        /// TreeView控件绑定数据
        /// </summary>
        /// <param name="treeNode">控件节点名称</param>
        /// <param name="dt">数据集</param>
        /// <param name="columnName">列名</param>
        public static void BindTreeView(TreeNode treeNode,DataTable dt,string columnName)
        {
            foreach(DataRow row in dt.Rows)
            {
                treeNode.Nodes.Add(row[columnName].ToString());
            }
        }
        /// <summary>
        /// LisbBox绑定数据
        /// </summary>
        /// <param name="listBox">控件名称</param>
        /// <param name="dt">数据集</param>
        /// <param name="columnName">列名</param>
        public static void BindListBox(ListBox listBox,DataTable dt,string columnName)
        {
            listBox.Items.Clear();
            for(int i = 0; i < dt.Rows.Count; i++)
            {
                listBox.Items.Add(dt.Rows[i][columnName].ToString());
            }
        }
        /// <summary>
        /// CheckedListBox数据绑定
        /// </summary>
        /// <param name="checkedList">控件名称</param>
        /// <param name="dt">数据集</param>
        /// <param name="columnName">列名</param>
        public static void BindCheckedListBox(CheckedListBox checkedList,DataTable dt,string columnName)
        {
            checkedList.Items.Clear();
            for(int i = 0; i < dt.Rows.Count; i++)
            {
                checkedList.Items.Add(dt.Rows[i][columnName].ToString());
            }
        }
        /// <summary>
        /// ListBox数据绑定与显示
        /// </summary>
        /// <param name="listBox">控件名称</param>
        /// <param name="dt">数据集</param>
        /// <param name="displayMember">显示名称</param>
        /// <param name="valueMember">存储名称</param>
        public static void BindListBox(ListBox listBox,DataTable dt,string displayMember,string valueMember)
        {
            listBox.DisplayMember = displayMember;
            listBox.ValueMember = valueMember;
            listBox.DataSource = dt;
        }
        /// <summary>
        /// 往ListBox中添加项
        /// </summary>
        /// <param name="listBox">控件名称</param>
        /// <param name="itemValue"></param>
        public static void AddItem(ListBox listBox,string itemValue)
        {
            for(int i = 0; i < listBox.Items.Count; i++)
            {
                if (listBox.Items[i].ToString() == itemValue)
                {
                    return;
                }
            }
            listBox.Items.Add(itemValue);
        }
        /// <summary>
        /// 判断添加的项是否存在ListBox中
        /// </summary>
        /// <param name="listBox">控件名称</param>
        /// <param name="itemValue"></param>
        /// <returns></returns>
        public static bool IsInListBox(ListBox listBox,string itemValue)
        {
            for(int i = 0; i < listBox.Items.Count; i++)
            {
                if (listBox.Items[i].ToString() == itemValue)
                {
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// 选择ListBox项
        /// </summary>
        /// <param name="listBox">控件名称</param>
        /// <param name="itemValue"></param>
        public static void SelectItem(ListBox listBox,string itemValue)
        {
            for(int i = 0; i < listBox.Items.Count; i++)
            {
                if (listBox.Items[i].ToString() == itemValue)
                {
                    listBox.SelectedItem = listBox.Items[i];
                    return;
                }
            }
        }
    }

 

控件帮助类

标签:datarow   node   static   mem   tab   combobox   判断   isp   checked   

原文地址:https://www.cnblogs.com/lxh168/p/13621346.html

版权声明:完美者 发表于 2020-09-17 16:14:44。
转载请注明:控件帮助类 | 完美导航

暂无评论

暂无评论...