Csharp:Windowsform using CheckedListBox Datasource

 /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ListboxCheckboxForm_Load(object sender, EventArgs e)
        {

            //设置CheckedListBox中第i项的Checked状态
            
            DataTable dt = new DataTable();
            dt.Columns.Add("id", typeof(Guid));
            dt.Columns.Add("name", typeof(string));
            dt.Rows.Add(Guid.NewGuid(), "geovindu");
            dt.Rows.Add(Guid.NewGuid(), "duf");
            dt.Rows.Add(Guid.NewGuid(), "涂聚文");
            dt.Rows.Add(Guid.NewGuid(), "tujwen");
            

            //checkedListBox1.Items.Add("");
            //checkedListBox1.Items.Insert(0, "");
            checkedListBox1.DataSource = dt;
            checkedListBox1.DisplayMember = "name";
            checkedListBox1.ValueMember = "id";

            checkedListBox1.SetItemCheckState(1, CheckState.Checked);
        }
        /// <summary>
        /// 獲取選擇的項
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {



            //1
            string checkedText = string.Empty;
            for (int i = 0; i < this.checkedListBox1.CheckedItems.Count; i++)
            {
                this.checkedListBox1.SetSelected(i, true);
                checkedText += (String.IsNullOrEmpty(checkedText) ? "" : ",") + this.checkedListBox1.GetItemText(this.checkedListBox1.Items[i]) + "[" +this.checkedListBox1.SelectedValue.ToString()+"]";
            }
            MessageBox.Show(checkedText);

            //2
            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {

                //如果checkedListBox1的第i项被选中,

                //则显示checkedListBox1对应的值

                if (checkedListBox1.GetItemChecked(i))
                {
                   // MessageBox.Show(checkedListBox1.GetItemText(checkedListBox1.Items[i]) + "[" + this.checkedListBox1.SelectedValue.ToString()+"]");
 
                }

            }

            //3
            string strCollected = string.Empty;

            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {

                if (checkedListBox1.GetItemChecked(i))
                {

                    if (strCollected == string.Empty)
                    {

                        strCollected = checkedListBox1.GetItemText(checkedListBox1.Items[i]);

                    }

                    else
                    {

                        strCollected = strCollected + "/" + checkedListBox1.GetItemText(checkedListBox1.Items[i]);

                    }

                }

            }
            //MessageBox.Show(strCollected);
        }


        /// <summary>
        /// 設定是否全選
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void select_all_CheckedChanged(object sender, EventArgs e)
        {
            if (select_all.Checked)
            {
                for (int j = 0; j < checkedListBox1.Items.Count; j++)
                    checkedListBox1.SetItemChecked(j, true);

            }
            else
            {
                for (int j = 0; j < checkedListBox1.Items.Count; j++)
                    checkedListBox1.SetItemChecked(j, false);

            }
        }
        /// <summary>
        /// 獲取選擇的項
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            string checkedText = string.Empty;
            for (int i = 0; i < this.checkedListBox1.Items.Count; i++)
            {
                if (this.checkedListBox1.GetItemChecked(i))
                {
                    this.checkedListBox1.SetSelected(i, true);
                    checkedText += (String.IsNullOrEmpty(checkedText) ? "" : ",") +"["+this.checkedListBox1.SelectedValue.ToString()+"]" + this.checkedListBox1.GetItemText(checkedListBox1.Items[i]);
                }
            }
            MessageBox.Show(checkedText);
        }
        /// <summary>
        /// 設置選擇項
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {


            checkedListBox1.DataSource = null;            
            DataTable dt = new DataTable();
            dt.Columns.Add("id", typeof(Guid));
            dt.Columns.Add("name", typeof(string));
            dt.Columns.Add("check", typeof(bool));

            dt.Rows.Add(Guid.NewGuid(), "geovindu",false);
            dt.Rows.Add(Guid.NewGuid(), "duf",true);
            dt.Rows.Add(Guid.NewGuid(), "涂聚文",false);
            dt.Rows.Add(Guid.NewGuid(), "tujwen",true);

            checkedListBox1.DataSource = dt;
            checkedListBox1.DisplayMember = "name";
            checkedListBox1.ValueMember = "id";  
            //
            for (int i = 0; i < dt.Rows.Count; i++)
            {

                checkedListBox1.SetItemChecked(i, (bool)dt.Rows[i]["check"]);
            }

        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值