码迷,mamicode.com
首页 > 其他好文 > 详细

控件帮助类

时间:2020-09-17 16:14:44      阅读:23      评论:0      收藏: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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!