zoukankan      html  css  js  c++  java
  • C#解决DataGridView中多选CheckBox列的功能的方案

    本文和大家介绍一下使用C#来实现解决DataGridView中CheckBox列的多选功能的方案,挺详细。
    由于DataGridView中的DataGridViewCheckBoxColumn默认无法执行多选操作,几经周折终于找到了解决方案,下面即是效果图和代码。
     
    (1):如下图,选择产品编号从00010到00015这几个产品,依次均可成功选中,但在默认情况下,只能选一个。

     (2):如下图,接着取消编号为00014的产品,这时候编号为00015的产品依旧为选中状态,但是在默认行为情况下,00015则为未选中状态。

     (3):继续取消编号为00013的产品,也没有问题。

     (4):继续取消编号为00012的产品,也没有问题。

     (5):再重新选中编号为00013的产品,也没有问题,这说明代码是有效的。

     
    如下代码:

     1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Text; 7 using System.Windows.Forms; 8 9 namespace TestDataGridViewCheckBox 10 { 11 public partial class Product : Form 12 { 13 /// <summary> 14 /// 初始化Product 15 /// </summary> 16 public Product() 17 { 18 InitializeComponent(); 19 20 //"gv"为DataGridView的名字 21 this.gv.Columns[0].Width = 50;//设置第一列的宽度 22 this.gv.AutoGenerateColumns = false;//不允许自动添加列 23 this.gv.AlternatingRowsDefaultCellStyle.BackColor = System.Drawing.Color.FromArgb(254, 222, 202);//设置交替行的背景颜色 24 this.gv.GridColor = System.Drawing.Color.FromArgb(248, 161, 109);//设置表格分割线的颜色 25 this.gv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;//设置表格列的填充方式 26 this.gv.CellValuePushed =new DataGridViewCellValueEventHandler(gv_CellValuePushed); 27 this.gv.CellValueNeeded =new DataGridViewCellValueEventHandler(gv_CellValueNeeded); 28 Init(); 29 } 30 31 /// <summary> 32 /// 将除选择列以外的其它列设为只读 33 /// </summary> 34 private void Init() 35 { 36 foreach (DataGridViewColumn c in gv.Columns) 37 { 38 if (c.Index != gv.Columns["colSelect"].Index)//"colSelect"为CheckBox列的名字 39 { 40 c.ReadOnly = true; 41 } 42 } 43 } 44 45 /// <summary> 46 /// 存储CheckBox状态的状态字典 47 /// </summary> 48 private static Dictionary<string, bool> checkState = new Dictionary<string, bool>(); 49 50 /// <summary> 51 /// 从状态字典中取出值,并更新到ChecBox 52 /// </summary> 53 private void gv_CellValuePushed(object sender, DataGridViewCellValueEventArgs e) 54 { 55 if(e.ColumnIndex == gv.Columns["colSelect"].Index && e.RowIndex >= 0) 56 { 57 //获取产品编号,"colProductID"为产品列的名字 58 string productId = gv.Rows[e.RowIndex].Cells["colProductId"].Value.ToString(); 59 if (checkState.ContainsKey(productId)) 60 e.Value = checkState[productId];//将状态字典中的值提取出来更新到CheckBox 61 else 62 e.Value = false;//默认CheckBox状态为未选中 63 } 64 } 65 66 /// <summary> 67 /// 将用户改变后的CheckBox的状态添加或更新到状态字典 68 /// </summary> 69 private void gv_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e) 70 { 71 if (e.ColumnIndex == gv.Columns["colSelect"].Index && e.RowIndex >= 0) 72 { 73 //获取产品编号 74 string productId = gv.Rows[e.RowIndex].Cells["colProductId"].Value.ToString(); 75 if (checkState.ContainsKey(productId)) 76 { 77 checkState[productId] = Convert.ToBoolean(e.Value);//将用户更改后的CheckBox的状态写入状态字典 78 } 79 else 80 { 81 checkState.Add(productId, Convert.ToBoolean(e.Value));//向状态字典中添加项 82 } 83 } 84 } 85 86 } 87 }

    在上面的这段程序中,最关键的就是CellValuePushed和CellValueNeeded两个事件,其中CellValueNeeded负责把用户执行的更改反映到自定义状态字典中,CellValuePushed则负责将状态字典中与之对应的值提取出来再返回到DataGridViewCheckBoxColumn上,这样DataGridViewCheckBoxColumn的状态值就可以即时而正确的反映出来。关于CellValuePushed和CellValueNeeded 这两个事件的权威参考,参见MSDN帮助文档。
    文章来自学IT网:http://www.xueit.com/cshare/show-9150-2.aspx
    文章来自学IT网:http://www.xueit.com/cshare/show-9150-2.aspx

    文章来自学IT网:http://www.xueit.com/cshare/show-9150-1.aspx

  • 相关阅读:
    Leetcode463. Island Perimeter
    C++ 编写的解码器小程序 map
    LeetCode706. Design HashMap
    LeetCode705. Design HashSet
    LeetCode804. Unique Morse Code Words
    c++后台开发 准备材料
    Scott Young-《如何高效学习》
    跳表和散列
    时间复杂度 log n
    第35题:LeetCode138. Copy List with Random Pointer
  • 原文地址:https://www.cnblogs.com/hl3292/p/1906342.html
Copyright © 2011-2022 走看看