如何在DataGrid中添加一列CheckedListBox用作选取该列和复选多列---WinForm DataGrid的TableStyles(表样式)属性应用...

本文介绍了如何在DataGrid中添加复选框列以实现选择和多选功能,并通过表样式映射提高了界面美观度。同时提供了一种更简便的数据源处理方法。此外,还附上了包含选中行删除功能的完整代码。

http://www.cnblogs.com/redjackwong/archive/2005/10/14/254840.html

今天群上一个MM问了一个问题,问题描述如下:
如何在DataGrid中添加一列CheckedListBox用作选取该列和复选多列。

当然既然是MM问的,而且我也对此很感兴趣,所以就接下来了。

此时我并不知道DataGrid的表样式映射功能,所以试了下将CheckedListBox和CheckBox直接放在DataGrid的后面-------结果,还真难看。后来想了想,既然这是MicroSoft的东西,这么常用的功能,应该在DataGrid中有集成。于是在属性栏中找,发现TableStyles(表样式)属性,凭直觉就感觉到这个是关键。点Collection子菜单,添加一个新表样式,在其属性中发现GridColumnStyles(Grid列样式),啊哈,就是它了。
当然使用表样式映射可以直接在属性栏中可视化完成,不过这样不够动态,以下解决过程采用代码编辑方式完成,便于动态改变和深入理解。

1、本例数据源采用xml格式,简单方便

None.gif<?xml version="1.0" standalone="yes"?>
None.gif
<Points>
None.gif  
<Point>
None.gif    
<X>0.00</X>
None.gif    
<Y>0.00</Y>
None.gif  
</Point>
None.gif  
<Point>
None.gif    
<X>78.50</X>
None.gif    
<Y>70.71</Y>
None.gif  
</Point>
None.gif  
<Point>
None.gif    
<X>157.10</X>
None.gif    
<Y>100.00</Y>
None.gif  
</Point>
None.gif  
<Point>
None.gif    
<X>235.60</X>
None.gif    
<Y>70.71</Y>
None.gif  
</Point>
None.gif  
<Point>
None.gif    
<X>314.10</X>
None.gif    
<Y>0.00</Y>
None.gif  
</Point>
None.gif  
<Point>
None.gif    
<X>392.60</X>
None.gif    
<Y>-70.71</Y>
None.gif  
</Point>
None.gif
</Points>

2、首先新建一个DataSet,并在构造函数中加载数据源

None.gifprivate DataSet ds = new DataSet();

 

None.gifpublic Form1()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            InitializeComponent();
InBlock.gif            ds.ReadXml(
"Points.xml");

3、添加一新列,作为显示为CheckBox的列,并将数据类型定义为bool,初始化为false。并绑定DataSetdataGrid1

None.gif            ds.Tables[0].Columns.Add("newc",false.GetType()).DefaultValue=false;
None.gif            
for(int i = 0; i < ds.Tables[0].Rows.Count; i++)
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif{
InBlock.gif                ds.Tables[
0].Rows[i]["newc"]=false;        //初始为未选中
ExpandedBlockEnd.gif            }

None.gif            dataGrid1.DataSource 
= ds.Tables[0].DefaultView;

4、新建一个表样式DataGridTableStyle,映射到DataSet的第一个表

None.gif            DataGridTableStyle ts1 = new DataGridTableStyle();
None.gif            ts1.MappingName 
= ds.Tables[0].TableName;            //映射到Tables[0]

5、新建与列数一样多的DataGridTextBoxColumn,映射到表原有的列(e.g.本例中的X和Y),表示以常规文本方式显示数据,并将其按顺序添加到表样式中

None.gif            DataGridTextBoxColumn t1 = new DataGridTextBoxColumn();
None.gif            t1.HeaderText 
= "X";                      //显示标题为X
None.gif            t1.MappingName 
= "X";                //映射到X列
None.gif            ts1.GridColumnStyles.Add(t1);     //添加到表样式中
None.gif
None.gif            DataGridTextBoxColumn t2 
= new DataGridTextBoxColumn();
None.gif            t2.HeaderText 
= "Y";
None.gif            t2.MappingName 
= "Y";
None.gif            ts1.GridColumnStyles.Add(t2);

6、新建一个DataGridBoolColumn,映射到新添加的列,表示以CheckBox样式显示bool型数据,并将其添加到表样式中

None.gif            DataGridBoolColumn b1 = new DataGridBoolColumn();
None.gif            b1.HeaderText 
= "My New Column";        //显示标题
None.gif            b1.MappingName 
= "newc";                       //映射列
None.gif            b1.NullText
="false";                                     //空值为false
None.gif            b1.TrueValue
=true;                                       //选中值为true
None.gif            b1.FalseValue
=false;                                     //未选中值为false
None.gif            b1.ReadOnly
=false;                                       //取消只读
None.gif            ts1.GridColumnStyles.Add(b1);                   //添加到表样式中

7、最后将DataGridTableStyle添加到DataGrid中

None.gif            dataGrid1.TableStyles.Add(ts1);

8、运行即可看到结果。如要对选中行进行处理,可以用一循环判断newc值,为true时即为选中该行,然后进行操作。

9、附全代码,包括选中行删除功能

None.gifusing System;
None.gif
using System.Drawing;
None.gif
using System.Collections;
None.gif
using System.ComponentModel;
None.gif
using System.Windows.Forms;
None.gif
using System.Data;
None.gif
None.gif
namespace Test
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public class Form1 : System.Windows.Forms.Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private DataSet ds = new DataSet();
InBlock.gif        
private System.Windows.Forms.DataGrid dataGrid1;
InBlock.gif        
private System.Windows.Forms.Button button1;
InBlock.gif
InBlock.gif        
private System.ComponentModel.Container components = null;
InBlock.gif
InBlock.gif        
public Form1()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            InitializeComponent();
InBlock.gif            ds.ReadXml(
"Points.xml");
InBlock.gif            ds.Tables[
0].Columns.Add("newc",false.GetType()).DefaultValue=false;
InBlock.gif            
for(int i = 0; i < ds.Tables[0].Rows.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ds.Tables[
0].Rows[i]["newc"]=false;
ExpandedSubBlockEnd.gif            }

InBlock.gif            dataGrid1.DataSource 
= ds.Tables[0].DefaultView;
InBlock.gif
InBlock.gif            DataGridTableStyle ts1 
= new DataGridTableStyle();
InBlock.gif            ts1.MappingName 
= ds.Tables[0].TableName;
InBlock.gif
InBlock.gif            DataGridTextBoxColumn t1 
= new DataGridTextBoxColumn();
InBlock.gif            t1.HeaderText 
= "X";
InBlock.gif            t1.MappingName 
= "X";
InBlock.gif            ts1.GridColumnStyles.Add(t1);
InBlock.gif
InBlock.gif            DataGridTextBoxColumn t2 
= new DataGridTextBoxColumn();
InBlock.gif            t2.HeaderText 
= "Y";
InBlock.gif            t2.MappingName 
= "Y";
InBlock.gif            ts1.GridColumnStyles.Add(t2);
InBlock.gif
InBlock.gif            DataGridBoolColumn b1 
= new DataGridBoolColumn();
InBlock.gif            b1.HeaderText 
= "My New Column";
InBlock.gif            b1.MappingName 
= "newc";
InBlock.gif            b1.NullText
="false";
InBlock.gif            b1.TrueValue
=true;
InBlock.gif            b1.FalseValue
=false;
InBlock.gif            b1.ReadOnly
=false;
InBlock.gif            ts1.GridColumnStyles.Add(b1);
InBlock.gif
InBlock.gif            dataGrid1.TableStyles.Add(ts1);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void Dispose( bool disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if( disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (components != null
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    components.Dispose();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.Dispose( disposing );
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.dataGrid1 = new System.Windows.Forms.DataGrid();
InBlock.gif            
this.button1 = new System.Windows.Forms.Button();
InBlock.gif            ((System.ComponentModel.ISupportInitialize)(
this.dataGrid1)).BeginInit();
InBlock.gif            
this.SuspendLayout();
InBlock.gif 
InBlock.gif            
// dataGrid1
InBlock.gif
            this.dataGrid1.DataMember = "";
InBlock.gif            
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
InBlock.gif            
this.dataGrid1.Location = new System.Drawing.Point(88);
InBlock.gif            
this.dataGrid1.Name = "dataGrid1";
InBlock.gif            
this.dataGrid1.Size = new System.Drawing.Size(280264);
InBlock.gif            
this.dataGrid1.TabIndex = 0;
InBlock.gif
InBlock.gif            
// button1
InBlock.gif
            this.button1.Location = new System.Drawing.Point(296248);
InBlock.gif            
this.button1.Name = "button1";
InBlock.gif            
this.button1.TabIndex = 1;
InBlock.gif            
this.button1.Text = "button1";
InBlock.gif            
this.button1.Click += new System.EventHandler(this.button1_Click);
InBlock.gif 
InBlock.gif            
// Form1
InBlock.gif
            this.AutoScaleBaseSize = new System.Drawing.Size(614);
InBlock.gif            
this.ClientSize = new System.Drawing.Size(376277);
InBlock.gif            
this.Controls.Add(this.button1);
InBlock.gif            
this.Controls.Add(this.dataGrid1);
InBlock.gif            
this.Name = "Form1";
InBlock.gif            
this.Text = "Form1";
InBlock.gif            ((System.ComponentModel.ISupportInitialize)(
this.dataGrid1)).EndInit();
InBlock.gif            
this.ResumeLayout(false);
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [STAThread]
InBlock.gif        
static void Main() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Application.Run(
new Form1());
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void button1_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
for(int i = 0; i < ds.Tables[0].Rows.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if ((bool)(ds.Tables[0].Rows[i]["newc"])==true)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    ds.Tables[
0].Rows[i].Delete();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

更好的解决方法是在数据源中本来就留有一列为bool型数据,这样就避免了程序中添加列,只需要直接映射就可以了。

如果有更好的办法请联系我,我也觉得这个方法挺复杂的。

转载于:https://www.cnblogs.com/lanru/archive/2010/08/27/1809733.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值