这里有个listview 的你可以参考思路:借助list来存储勾选项,listview中的行项每勾选一个就往list插入一条记录,取消勾选则从list中移除记录,当list.Count与listview的行数相同是则表示全部选择
listview放在SmobilerForm.cs中
- List<string> selectItem = new List<string>();//通过这个list来记录已勾选的数据行id
- /// <summary>
- /// 添加勾选项
- /// </summary>
- /// <param name="item"></param>
- public void AddSelectItem(string item)
- {
- if (!selectItem.Contains(item))
- selectItem.Add(item);
- }
- /// <summary>
- /// 取消选择
- /// </summary>
- /// <param name="item"></param>
- public void RemoveSelectItem(string item)
- {
- if (selectItem.Contains(item))
- selectItem.Remove(item);
- }
- /// <summary>
- /// 改变checkbox状态
- /// </summary>
- public void changeState()
- {
- if (selectItem.Count == listView1.Rows.Count)//selectItem的数量和listview.Rows的数量一致表示全选
- checkBox1.Checked = true;
- else
- checkBox1.Checked = false;
- }
- /// <summary>
- /// checkbox点击事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void checkBox1_CheckedChanged(object sender, EventArgs e)
- {
- if (checkBox1.Checked)
- {
- foreach (ListViewRow row in listView1.Rows)//遍历listview.Rows
- {
- //(SmobilerUserControl1)row.Control即listview行模板
- ((SmobilerUserControl1)row.Control).checkBox1.Checked = true;//改变listview模板里中的checkbox值
- AddSelectItem(((SmobilerUserControl1)row.Control).checkBox1.BindDataValue.ToString());//获取模板类中的checkbox的DataMember
- }
- }
- else
- {
- foreach (ListViewRow row in listView1.Rows)
- {
- ((SmobilerUserControl1)row.Control).checkBox1.Checked = false;
- RemoveSelectItem(((SmobilerUserControl1)row.Control).checkBox1.BindDataValue.ToString());
- }
- }
复制代码
listview的,模板类为SmobilerUserControl1.cs模板类使用(SmobilerForm1)this.Form来调用SmobilerForm1的属性、方法,将数据传给SMobilerForm1
- private void checkBox1_CheckedChanged(object sender, EventArgs e)
- {
- SmobilerForm1 frm = (SmobilerForm1)this.Form;//获取listview所在窗体
- if (checkBox1.Checked)
- {
- frm.AddSelectItem(checkBox1.BindDataValue.ToString());
- }
- else
- {
- frm.RemoveSelectItem(checkBox1.BindDataValue.ToString());
- }
- frm.changeState();
- }
复制代码
|