Csharp:Windowsform using CheckedListBox Datasource

复制代码
  1         /// <summary>
  2         /// 
  3         /// </summary>
  4         /// <param name="sender"></param>
  5         /// <param name="e"></param>
  6         private void ListboxCheckboxForm_Load(object sender, EventArgs e)
  7         {
  8 
  9             //设置CheckedListBox中第i项的Checked状态
 10             
 11             DataTable dt = new DataTable();
 12             dt.Columns.Add("id", typeof(Guid));
 13             dt.Columns.Add("name", typeof(string));
 14             dt.Rows.Add(Guid.NewGuid(), "geovindu");
 15             dt.Rows.Add(Guid.NewGuid(), "duf");
 16             dt.Rows.Add(Guid.NewGuid(), "涂聚文");
 17             dt.Rows.Add(Guid.NewGuid(), "tujwen");
 18             
 19 
 20             //checkedListBox1.Items.Add("");
 21             //checkedListBox1.Items.Insert(0, "");
 22             checkedListBox1.DataSource = dt;
 23             checkedListBox1.DisplayMember = "name";
 24             checkedListBox1.ValueMember = "id";
 25 
 26             checkedListBox1.SetItemCheckState(1, CheckState.Checked);
 27         }
 28         /// <summary>
 29         /// 獲取選擇的項
 30         /// </summary>
 31         /// <param name="sender"></param>
 32         /// <param name="e"></param>
 33         private void button1_Click(object sender, EventArgs e)
 34         {
 35 
 36 
 37 
 38             //1
 39             string checkedText = string.Empty;
 40             for (int i = 0; i < this.checkedListBox1.CheckedItems.Count; i++)
 41             {
 42                 this.checkedListBox1.SetSelected(i, true);
 43                 checkedText += (String.IsNullOrEmpty(checkedText) ? "" : ",") + this.checkedListBox1.GetItemText(this.checkedListBox1.Items[i]) + "[" +this.checkedListBox1.SelectedValue.ToString()+"]";
 44             }
 45             MessageBox.Show(checkedText);
 46 
 47             //2
 48             for (int i = 0; i < checkedListBox1.Items.Count; i++)
 49             {
 50 
 51                 //如果checkedListBox1的第i项被选中,
 52 
 53                 //则显示checkedListBox1对应的值
 54 
 55                 if (checkedListBox1.GetItemChecked(i))
 56                 {
 57                    // MessageBox.Show(checkedListBox1.GetItemText(checkedListBox1.Items[i]) + "[" + this.checkedListBox1.SelectedValue.ToString()+"]");
 58  
 59                 }
 60 
 61             }
 62 
 63             //3
 64             string strCollected = string.Empty;
 65 
 66             for (int i = 0; i < checkedListBox1.Items.Count; i++)
 67             {
 68 
 69                 if (checkedListBox1.GetItemChecked(i))
 70                 {
 71 
 72                     if (strCollected == string.Empty)
 73                     {
 74 
 75                         strCollected = checkedListBox1.GetItemText(checkedListBox1.Items[i]);
 76 
 77                     }
 78 
 79                     else
 80                     {
 81 
 82                         strCollected = strCollected + "/" + checkedListBox1.GetItemText(checkedListBox1.Items[i]);
 83 
 84                     }
 85 
 86                 }
 87 
 88             }
 89             //MessageBox.Show(strCollected);
 90         }
 91 
 92 
 93         /// <summary>
 94         /// 設定是否全選
 95         /// </summary>
 96         /// <param name="sender"></param>
 97         /// <param name="e"></param>
 98         private void select_all_CheckedChanged(object sender, EventArgs e)
 99         {
100             if (select_all.Checked)
101             {
102                 for (int j = 0; j < checkedListBox1.Items.Count; j++)
103                     checkedListBox1.SetItemChecked(j, true);
104 
105             }
106             else
107             {
108                 for (int j = 0; j < checkedListBox1.Items.Count; j++)
109                     checkedListBox1.SetItemChecked(j, false);
110 
111             }
112         }
113         /// <summary>
114         /// 獲取選擇的項
115         /// </summary>
116         /// <param name="sender"></param>
117         /// <param name="e"></param>
118         private void button2_Click(object sender, EventArgs e)
119         {
120             string checkedText = string.Empty;
121             for (int i = 0; i < this.checkedListBox1.Items.Count; i++)
122             {
123                 if (this.checkedListBox1.GetItemChecked(i))
124                 {
125                     this.checkedListBox1.SetSelected(i, true);
126                     checkedText += (String.IsNullOrEmpty(checkedText) ? "" : ",") +"["+this.checkedListBox1.SelectedValue.ToString()+"]" + this.checkedListBox1.GetItemText(checkedListBox1.Items[i]);
127                 }
128             }
129             MessageBox.Show(checkedText);
130         }
131         /// <summary>
132         /// 設置選擇項
133         /// </summary>
134         /// <param name="sender"></param>
135         /// <param name="e"></param>
136         private void button3_Click(object sender, EventArgs e)
137         {
138 
139 
140             checkedListBox1.DataSource = null;            
141             DataTable dt = new DataTable();
142             dt.Columns.Add("id", typeof(Guid));
143             dt.Columns.Add("name", typeof(string));
144             dt.Columns.Add("check", typeof(bool));
145 
146             dt.Rows.Add(Guid.NewGuid(), "geovindu",false);
147             dt.Rows.Add(Guid.NewGuid(), "duf",true);
148             dt.Rows.Add(Guid.NewGuid(), "涂聚文",false);
149             dt.Rows.Add(Guid.NewGuid(), "tujwen",true);
150 
151             checkedListBox1.DataSource = dt;
152             checkedListBox1.DisplayMember = "name";
153             checkedListBox1.ValueMember = "id";  
154             //
155             for (int i = 0; i < dt.Rows.Count; i++)
156             {
157 
158                 checkedListBox1.SetItemChecked(i, (bool)dt.Rows[i]["check"]);
159             }
160 
161         }
162     }
复制代码

 示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//checkedListBox 綁定數據
chklistClerkRelationTypeID.DataSource = rebll.SelectDataTableAll();
chklistClerkRelationTypeID.DisplayMember = "RelationName";
chklistClerkRelationTypeID.ValueMember = "RelationId";
 
if (choose == ChooseOperation.OperationName.Edit)
{
 
    this.txtClerkRelationDesc.Text = ClerkRelationshipStaticInfo.ClerkRelationDesc;
    this.txtClerkRelationName.Text = ClerkRelationshipStaticInfo.ClerkRelationName;
    this.txtClerkRelationTel.Text = ClerkRelationshipStaticInfo.ClerkRelationTel;
    this.dateClerkBirthday.Value = ClerkRelationshipStaticInfo.ClerkBirthday;
 
    //已先擇的項目 塗聚文(Geovin Du)
    string strcheck = ClerkRelationshipStaticInfo.ClerkRelationTypeID; // 1,3
    for (int j = 0; j < chklistClerkRelationTypeID.Items.Count; j++)
    {
        DataRowView dv = ((DataRowView)chklistClerkRelationTypeID.Items[j]);
           string id=dv["RelationId"].ToString();
           if (Geovin.Du.Common.StringConvert.getSearch(id,strcheck))
               chklistClerkRelationTypeID.SetItemChecked(j, true);//true改为false为没有选中。
    }  

  

 

posted @   ®Geovin Du Dream Park™  阅读(886)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
< 2013年6月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 1 2 3 4 5 6
点击右上角即可分享
微信分享提示