CheckedListBox与CheckedListBox联动

包括保存和加载

//查找业务类型
DataTable dtyewu = sb.SelectSyscode(0, true);

if (dtyewu.Rows.Count > 0)
{

flagcbb = false;
clb_yewu.DataSource = dtyewu;
clb_yewu.DisplayMember = "name";
clb_yewu.ValueMember = "id";
DataRowView dr = clb_yewu.Items[0] as DataRowView;
dtyewuleixingbind(dr["id"].ToString(), true);

flagcbb = true;

}

biz = rc.Bizid.Split(‘;‘);
pit = rc.Pid.Split(‘;‘);
selectcbb();
loadbing();

private void selectcbb()
{
int m = 0;
for (int j = 0; j < clb_yewu.Items.Count; j++)
{
DataRowView dr = clb_yewu.Items[j] as DataRowView;

for (int i = 0; i < biz.Length; i++)
{
if (dr["id"].ToString() == biz[i])
{
clb_yewu.SetItemChecked(j, true);
break;
}
}

}
}

/// <summary>
/// 所有平台信息
/// </summary>
private void selectPingtai()
{
string m;
string[] pt;
DataTable dtapt = sb.SelectAllPingTai();

dtallpt.Columns.Add("id", typeof(Int32));
dtallpt.Columns.Add("cd", typeof(string));
dtallpt.Columns.Add("name", typeof(string));
for (int i = 0; i < dtapt.Rows.Count; i++)
{

m = dtapt.Rows[i][0].ToString();
pt = m.Split(‘;‘);
foreach (string ptf in pt)
{
DataRow dr = dtallpt.NewRow();
dr["id"] = dtapt.Rows[i][1].ToString();
dr["cd"] = ptf;
dr["name"] = dtapt.Rows[i][2].ToString();
dtallpt.Rows.Add(dr);
}
}
}

 private void clb_yewu_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (flagcbb)
            {
                string cdd = "";
                int m = clb_yewu.SelectedIndex;
                if (clb_yewu.CheckedItems.IndexOf(clb_yewu.Items[m]) >= 0)
                    dtyewuleixingbind(clb_yewu.SelectedValue.ToString(), true);
                if (clb_yewu.CheckedItems.IndexOf(clb_yewu.Items[m]) <= 0)
                {
                    dtyewuleixingbind(clb_yewu.SelectedValue.ToString(), false);
                }
            }
        }
        private void clb_SelectedIndexChanged(object sender, EventArgs e)
        {
            int m = ((CheckedListBox)sender).SelectedIndex;
            if (((CheckedListBox)sender).CheckedItems.IndexOf(((CheckedListBox)sender).Items[m]) >= 0)
            {
                for (int j = 0; j < clb_yewu.Items.Count; j++)
                {

                    DataRowView dr = clb_yewu.Items[j] as DataRowView;
                    if (dr["id"].ToString() == ((CheckedListBox)sender).Name)
                    {
                        clb_yewu.SetItemChecked(j, true);
                        break;
                    }
                }

            }

        }
        /// <summary>
        /// 筛选平台
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private DataTable selectdypt(string id)
        {
            DataRow[] drArr = dtallpt.Select("cd =‘" + id + "‘");
            DataTable dtNew = dtallpt.Clone();
            for (int i = 0; i < drArr.Length; i++)
            {
                dtNew.ImportRow(drArr[i]);

            }
            return dtNew;
        }
        /// <summary>
        /// 绑定平台
        /// </summary>
        /// <param name="id"></param>
        private void dtyewuleixingbind(string id, bool select)  //xu
        {
            //DataTable dtpingtai = sb.SelectPingtai(cdd);
            DataTable dtpingtai = selectdypt(id);
            bool flag = false;
            foreach (Control con in panel2.Controls)
            {
                if (con.Name == id)
                {
                    con.Visible = true;
                    flag = true;
                    if (!select)
                    {
                        for (int j = 0; j < ((CheckedListBox)con).Items.Count; j++)
                            ((CheckedListBox)con).SetItemChecked(j, false);
                    }
                }
                else
                {
                    con.Visible = false;
                }

            }
            if (!flag)
            {
                CheckedListBox clb_yewu = new CheckedListBox();
                clb_yewu.Name = id;
                clb_yewu.Visible = true;
                if (dtpingtai.Rows.Count > 0)
                {
                    clb_yewu.DataSource = dtpingtai;
                    clb_yewu.DisplayMember = "name";
                    clb_yewu.ValueMember = "id";
                    clb_yewu.SelectedIndexChanged += new EventHandler(clb_SelectedIndexChanged);
                }
                Size ss = new Size(200, 150);
                clb_yewu.Size = ss;
                clb_yewu.CheckOnClick = true;
                panel2.Controls.Add(clb_yewu);

            }

        }

        #region 修改个人信息平台加载
        /// <summary>
        /// 平台数据加载
        /// </summary>
        private void loadbing()
        {
            if (uid > 0)
            {
                for (int z = 0; z < biz.Length; z++)
                {
                    DataTable dtpingtai = selectdypt(biz[z]);
                    CheckedListBox clb_yewu = new CheckedListBox();
                    clb_yewu.Name = biz[z];
                    clb_yewu.Visible = true;
                    if (dtpingtai.Rows.Count > 0)
                    {
                        clb_yewu.DataSource = dtpingtai;
                        clb_yewu.DisplayMember = "name";
                        clb_yewu.ValueMember = "id";
                    }

                    Size ss = new Size(200, 150);
                    clb_yewu.Size = ss;
                    panel2.Controls.Add(clb_yewu);

                }
                ptbinding();
            }
        }
        /// <summary>
        /// 选中对应数据库项
        /// </summary>
        private void ptbinding()
        {
            int i = 0;

            foreach (Control c in panel2.Controls)
            {
                i = 0;
                for (int j = 0; j < ((CheckedListBox)c).Items.Count; j++)
                {
                    DataRowView dr = ((CheckedListBox)c).Items[j] as DataRowView;
                    if (pit != null)
                        foreach (string pt in pit)
                        {
                            string[] p = pt.Split(‘-‘);
                            if (dr["cd"].ToString() == p[0])
                                if (dr["id"].ToString() == p[1])
                                {

                                    ((CheckedListBox)c).SetItemChecked(i, true);
                                    break;
                                }

                        }
                    i++;

                }
            }
        }

  

CheckedListBox与CheckedListBox联动

时间: 2024-10-07 02:18:21

CheckedListBox与CheckedListBox联动的相关文章

C# CheckedListBox控件的使用方法

1. 加入项 checkedListBox1.Items.Add("蓝色"); checkedListBox1.Items.Add("红色"); checkedListBox1.Items.Add("黄色"); 2. 推断第i项是否选中,选中为true,否则为false if(checkedListBox1.GetItemChecked(i)) {      return true; } else {      return false; } 3

Winform开发常用控件之Checkbox和CheckedListBox

Winform的开发基本都是基于控件事件的,也就是事件驱动型的. 多选框的放置和值的获取有很多种,这里介绍几个简单常用的方法 1.直接放置Checkbox,并获取Checkbox的值 上图 做法也非常简单,就是在form中放置多个checkbox,然后循环遍历,检查其checked没有,然后将checkbox.text拼接到一起,赋值给label,当然实际开发中,我们会将选取的值放入数据库中.上代码 private void button1_Click(object sender, EventA

Winform中checklistbox控件的常用方法

Winform中checklistbox控件的常用方法最近用到checklistbox控件,在使用其过程中,收集了其相关的代码段1.添加项checkedListBox1.Items.Add("蓝色"); checkedListBox1.Items.Add("红色"); checkedListBox1.Items.Add("黄色"); 2. 判断第i项是否选中,选中为true,否则为falseif(checkedListBox1.GetItemCh

C# checkboxlist的使用

最近做项目,需要用到checklistbox这一控件,由于对这一控件并不是很熟悉,导致在此浪费了一点时间. http://www.cnblogs.com/hongfei/archive/2012/12/21/2828408.html for (int i = 0; i < checkedListBox1.Items.Count; i++) { if (checkedListBox1.GetItemChecked(i)) { MessageBox.Show(checkedListBox1.GetI

C# checklistbox控件用法总结(怎样得到多选的值,以及动态加载数据)

C# checklistbox控件用法总结(怎样得到多选的值,以及动态加载数据) 一般认为:foreach (object obj in checkedListBox1.SelectedItems)即可遍历选中的值. 其实这里遍历的只是高亮的值并不是打勾的值.遍历打勾的值要用下面的代码: for (int i = 0; i < checkedListBox1.Items.Count; i++) { if (checkedListBox1.GetItemChecked(i)) { MessageB

分享两种实现Winform程序的多语言支持的解决方案

因公司业务需要,需要将原有的ERP系统加上支持繁体语言,但不能改变原有的编码方式,即:普通程序员感受不到编码有什么不同.经过我与几个同事的多番沟通,确定了以下两种方案: 方案一:在窗体基类中每次加载并显示窗体时,会自动递归遍历含文本显示的控件(Button,CheckBox,GroupBox,Label,LinkLabel,TextBox,StatusStrip,TabPage,ToolStrip,RadioButton,DateTimePicker,DataGridView,CheckedLi

CheckedListBox&#160; 数据绑定

CheckedListBox  数据绑定方式有多总,常用的绑定方式总结如下: 1.  Items.Add 通过 Itemes.Add 方法来向 CheckedListBox  中添加项 2. DataSource属性,DisplayMember和ValueMemeber 来设置文本显示 CheckedListBox 和 ListBox  相似,但 VS 的智能感应窗口并不提示 DataSoource 属性及 DisplayMember 和 ValueMember , 但这些属性是实际存在并可用的

checkedListBox的数据绑定

string strq = "select Stru_chName,Stru_enName from T_StuRowName;"; SqlCommand command = new SqlCommand(strq, con); SqlDataReader reader = command.ExecuteReader(); BindingSource bs = new BindingSource(); bs.DataSource = reader; checkedListBox1.Da

checkedListBox

checkedListBox一键删除多个选中items private void button3_Click(object sender, EventArgs e) { for (int i = 0; i < checkedListBox1.Items.Count; i++) { if (checkedListBox1.GetItemChecked(i)) { checkedListBox1.Items.RemoveAt(i); i--; } } checkedListBox反选 private