对DataGrid的初步了解

本文介绍了DataGrid的基础使用方法,包括如何加载XML数据并显示在DataGrid中,还演示了如何设置DataGrid的各种样式和特性,如自定义列类型等。
由于以前对DataGrid的了解相当初浅,只能用来显示数据
今天做了一下对DataGrid的深入学习
通过在网上搜索相关资料,总算能让例子达到自己的初步要求
也开始明白为什么别人说学习是一个循序渐进的过程
主要用到的一些代码:
None.gif using  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
using  System.Xml;
None.gif
None.gif
namespace  ReadAndWrite
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Form1 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Form1 : System.Windows.Forms.Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private System.Windows.Forms.Panel panel1;
InBlock.gif        
private System.Windows.Forms.DataGrid dataGrid1;
InBlock.gif        
private System.Windows.Forms.Button btn_BroswerXML;
InBlock.gif        
private System.Data.DataSet ds;
InBlock.gif        
private System.Windows.Forms.RichTextBox richTextBox1;
InBlock.gif        
private System.Windows.Forms.Button button1;
InBlock.gif        
private System.Windows.Forms.DataGridTableStyle dataGridTableStyle1;
InBlock.gif        
private System.Windows.Forms.DataGridBoolColumn dataGridBoolColumn1;
InBlock.gif        
private System.Windows.Forms.Button button2;
InBlock.gif
InBlock.gif        
private bool flag =false;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 必需的设计器变量。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private System.ComponentModel.Container components = null;
InBlock.gif
InBlock.gif        
public Form1()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// Windows 窗体设计器支持所必需的
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif
InBlock.gif            
//
InBlock.gif            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 清理所有正在使用的资源。
ExpandedSubBlockEnd.gif        
/// </summary>

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
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.Resources.ResourceManager resources 
= new System.Resources.ResourceManager(typeof(Form1));
InBlock.gif            
this.panel1 = new System.Windows.Forms.Panel();
InBlock.gif            
this.dataGrid1 = new System.Windows.Forms.DataGrid();
InBlock.gif            
this.dataGridTableStyle1 = new System.Windows.Forms.DataGridTableStyle();
InBlock.gif            
this.dataGridBoolColumn1 = new System.Windows.Forms.DataGridBoolColumn();
InBlock.gif            
this.ds = new System.Data.DataSet();
InBlock.gif            
this.btn_BroswerXML = new System.Windows.Forms.Button();
InBlock.gif            
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
InBlock.gif            
this.button1 = new System.Windows.Forms.Button();
InBlock.gif            
this.button2 = new System.Windows.Forms.Button();
InBlock.gif            
this.panel1.SuspendLayout();
InBlock.gif            ((System.ComponentModel.ISupportInitialize)(
this.dataGrid1)).BeginInit();
InBlock.gif            ((System.ComponentModel.ISupportInitialize)(
this.ds)).BeginInit();
InBlock.gif            
this.SuspendLayout();
InBlock.gif            
// 
InBlock.gif            
// panel1
InBlock.gif            
// 
InBlock.gif
            this.panel1.Controls.Add(this.dataGrid1);
InBlock.gif            
this.panel1.Dock = System.Windows.Forms.DockStyle.Left;
InBlock.gif            
this.panel1.Location = new System.Drawing.Point(00);
InBlock.gif            
this.panel1.Name = "panel1";
InBlock.gif            
this.panel1.Size = new System.Drawing.Size(448286);
InBlock.gif            
this.panel1.TabIndex = 0;
InBlock.gif            
// 
InBlock.gif            
// dataGrid1
InBlock.gif            
// 
InBlock.gif
            this.dataGrid1.AlternatingBackColor = System.Drawing.Color.Lavender;
InBlock.gif            
this.dataGrid1.BackColor = System.Drawing.Color.WhiteSmoke;
InBlock.gif            
this.dataGrid1.BackgroundColor = System.Drawing.Color.LightGray;
InBlock.gif            
this.dataGrid1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
InBlock.gif            
this.dataGrid1.CaptionBackColor = System.Drawing.Color.LightSteelBlue;
InBlock.gif            
this.dataGrid1.CaptionForeColor = System.Drawing.Color.MidnightBlue;
InBlock.gif            
this.dataGrid1.CaptionText = "XML文档相关属性";
InBlock.gif            
this.dataGrid1.DataMember = "";
InBlock.gif            
this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Fill;
InBlock.gif            
this.dataGrid1.FlatMode = true;
InBlock.gif            
this.dataGrid1.Font = new System.Drawing.Font("Tahoma", 8F);
InBlock.gif            
this.dataGrid1.ForeColor = System.Drawing.Color.MidnightBlue;
InBlock.gif            
this.dataGrid1.GridLineColor = System.Drawing.Color.Gainsboro;
InBlock.gif            
this.dataGrid1.GridLineStyle = System.Windows.Forms.DataGridLineStyle.None;
InBlock.gif            
this.dataGrid1.HeaderBackColor = System.Drawing.Color.MidnightBlue;
InBlock.gif            
this.dataGrid1.HeaderFont = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Bold);
InBlock.gif            
this.dataGrid1.HeaderForeColor = System.Drawing.Color.WhiteSmoke;
InBlock.gif            
this.dataGrid1.LinkColor = System.Drawing.Color.Teal;
InBlock.gif            
this.dataGrid1.Location = new System.Drawing.Point(00);
InBlock.gif            
this.dataGrid1.Name = "dataGrid1";
InBlock.gif            
this.dataGrid1.ParentRowsBackColor = System.Drawing.Color.Gainsboro;
InBlock.gif            
this.dataGrid1.ParentRowsForeColor = System.Drawing.Color.MidnightBlue;
InBlock.gif            
this.dataGrid1.SelectionBackColor = System.Drawing.Color.CadetBlue;
InBlock.gif            
this.dataGrid1.SelectionForeColor = System.Drawing.Color.WhiteSmoke;
InBlock.gif            
this.dataGrid1.Size = new System.Drawing.Size(448286);
InBlock.gif            
this.dataGrid1.TabIndex = 1;
ExpandedSubBlockStart.gifContractedSubBlock.gif            
this.dataGrid1.TableStyles.AddRange(new System.Windows.Forms.DataGridTableStyle[] dot.gif{
ExpandedSubBlockEnd.gif                                                                                                  
this.dataGridTableStyle1}
);
InBlock.gif            
// 
InBlock.gif            
// dataGridTableStyle1
InBlock.gif            
// 
InBlock.gif
            this.dataGridTableStyle1.DataGrid = this.dataGrid1;
ExpandedSubBlockStart.gifContractedSubBlock.gif            
this.dataGridTableStyle1.GridColumnStyles.AddRange(new System.Windows.Forms.DataGridColumnStyle[] dot.gif{
ExpandedSubBlockEnd.gif                                                                                                                  
this.dataGridBoolColumn1}
);
InBlock.gif            
this.dataGridTableStyle1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
InBlock.gif            
this.dataGridTableStyle1.MappingName = "";
InBlock.gif            
// 
InBlock.gif            
// dataGridBoolColumn1
InBlock.gif            
// 
InBlock.gif
            this.dataGridBoolColumn1.FalseValue = false;
InBlock.gif            
this.dataGridBoolColumn1.MappingName = "";
InBlock.gif            
this.dataGridBoolColumn1.NullValue = ((object)(resources.GetObject("dataGridBoolColumn1.NullValue")));
InBlock.gif            
this.dataGridBoolColumn1.TrueValue = true;
InBlock.gif            
this.dataGridBoolColumn1.Width = 75;
InBlock.gif            
// 
InBlock.gif            
// ds
InBlock.gif            
// 
InBlock.gif
            this.ds.DataSetName = "NewDataSet";
InBlock.gif            
this.ds.Locale = new System.Globalization.CultureInfo("zh-CN");
InBlock.gif            
// 
InBlock.gif            
// btn_BroswerXML
InBlock.gif            
// 
InBlock.gif
            this.btn_BroswerXML.Location = new System.Drawing.Point(4568);
InBlock.gif            
this.btn_BroswerXML.Name = "btn_BroswerXML";
InBlock.gif            
this.btn_BroswerXML.Size = new System.Drawing.Size(9623);
InBlock.gif            
this.btn_BroswerXML.TabIndex = 1;
InBlock.gif            
this.btn_BroswerXML.Text = "选择XML文档…";
InBlock.gif            
this.btn_BroswerXML.Click += new System.EventHandler(this.btn_BroswerXML_Click);
InBlock.gif            
// 
InBlock.gif            
// richTextBox1
InBlock.gif            
// 
InBlock.gif
            this.richTextBox1.Location = new System.Drawing.Point(45648);
InBlock.gif            
this.richTextBox1.Name = "richTextBox1";
InBlock.gif            
this.richTextBox1.Size = new System.Drawing.Size(104144);
InBlock.gif            
this.richTextBox1.TabIndex = 2;
InBlock.gif            
this.richTextBox1.Text = "";
InBlock.gif            
// 
InBlock.gif            
// button1
InBlock.gif            
// 
InBlock.gif
            this.button1.Location = new System.Drawing.Point(472200);
InBlock.gif            
this.button1.Name = "button1";
InBlock.gif            
this.button1.TabIndex = 3;
InBlock.gif            
this.button1.Text = "Form2";
InBlock.gif            
this.button1.Click += new System.EventHandler(this.button1_Click);
InBlock.gif            
// 
InBlock.gif            
// button2
InBlock.gif            
// 
InBlock.gif
            this.button2.Location = new System.Drawing.Point(464240);
InBlock.gif            
this.button2.Name = "button2";
InBlock.gif            
this.button2.Size = new System.Drawing.Size(8823);
InBlock.gif            
this.button2.TabIndex = 4;
InBlock.gif            
this.button2.Text = "导入内容预览";
InBlock.gif            
this.button2.Click += new System.EventHandler(this.button2_Click);
InBlock.gif            
// 
InBlock.gif            
// Form1
InBlock.gif            
// 
InBlock.gif
            this.AutoScaleBaseSize = new System.Drawing.Size(614);
InBlock.gif            
this.ClientSize = new System.Drawing.Size(576286);
InBlock.gif            
this.Controls.Add(this.button2);
InBlock.gif            
this.Controls.Add(this.button1);
InBlock.gif            
this.Controls.Add(this.richTextBox1);
InBlock.gif            
this.Controls.Add(this.btn_BroswerXML);
InBlock.gif            
this.Controls.Add(this.panel1);
InBlock.gif            
this.Name = "Form1";
InBlock.gif            
this.Text = "Form1";
InBlock.gif            
this.Load += new System.EventHandler(this.Form1_Load);
InBlock.gif            
this.panel1.ResumeLayout(false);
InBlock.gif            ((System.ComponentModel.ISupportInitialize)(
this.dataGrid1)).EndInit();
InBlock.gif            ((System.ComponentModel.ISupportInitialize)(
this.ds)).EndInit();
InBlock.gif            
this.ResumeLayout(false);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 应用程序的主入口点。
ExpandedSubBlockEnd.gif        
/// </summary>

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 btn_BroswerXML_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                OpenFileDialog openFileDialog 
= new OpenFileDialog();
InBlock.gif                openFileDialog.InitialDirectory 
=@"F:\";
InBlock.gif                openFileDialog.Filter 
="XML文件|*.xml";
InBlock.gif                openFileDialog.RestoreDirectory 
=true;
InBlock.gif                openFileDialog.FilterIndex 
=1;
InBlock.gif                
if(openFileDialog.ShowDialog()==DialogResult.OK)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    flag 
=true;
InBlock.gif                    ds.ReadXml(openFileDialog.FileName);
InBlock.gif                    
this.dataGrid1.DataSource =ds.Tables["Item"];
ExpandedSubBlockEnd.gif                }

InBlock.gif                ds.Tables[
"Item"].Columns.Add("Current",typeof(bool));
InBlock.gif                
//STEP 1: Create a DataTable style object and set properties if required.
InBlock.gif
                DataGridTableStyle ts1 = new DataGridTableStyle();
InBlock.gif
InBlock.gif                
//specify the table from dataset (required step)
InBlock.gif
                ts1.MappingName = "Item";
InBlock.gif          
InBlock.gif                
// Set other properties (optional step)
InBlock.gif
                ts1.AlternatingBackColor = Color.LightBlue;
InBlock.gif
InBlock.gif                
//STEP 2: Create a string column and add it to the tablestyle
InBlock.gif
                DataGridColumnStyle TextCol = new DataGridTextBoxColumn();
InBlock.gif                TextCol.MappingName 
= "custName"//from dataset table
InBlock.gif
                TextCol.HeaderText = "Customer Name";
InBlock.gif                TextCol.Width 
= 100;
InBlock.gif                ts1.GridColumnStyles.Add(TextCol);
InBlock.gif
InBlock.gif                
//STEP 3: Create an int column style and add it to the tablestyle
InBlock.gif                
//this requires setting the format for the column through its property descriptor
InBlock.gif
                PropertyDescriptorCollection pdc = this.BindingContext
InBlock.gif                    [ds, 
"Item"].GetItemProperties();
InBlock.gif
InBlock.gif                
//now created a formated column using the pdc
InBlock.gif
//                DataGridTextBoxColumn csIDInt = 
InBlock.gif
//                    new DataGridTextBoxColumn(pdc["CustID"], "i", true);
InBlock.gif
//                csIDInt.MappingName = "CustID";
InBlock.gif
//                csIDInt.HeaderText = "CustID";
InBlock.gif
//                csIDInt.Width = 50;
InBlock.gif
//                ts1.GridColumnStyles.Add(csIDInt);
InBlock.gif
                DataGridTextBoxColumn csIDInt = new DataGridTextBoxColumn();
InBlock.gif                csIDInt.MappingName
="SID";
InBlock.gif                csIDInt.HeaderText
="SID";
InBlock.gif                csIDInt.Width
=100;
InBlock.gif                ts1.GridColumnStyles.Add(csIDInt);
InBlock.gif                
InBlock.gif                csIDInt 
= new DataGridTextBoxColumn();
InBlock.gif                csIDInt.MappingName
="TID";
InBlock.gif                csIDInt.HeaderText
="TID";
InBlock.gif                csIDInt.Width
=100;
InBlock.gif                ts1.GridColumnStyles.Add(csIDInt);
InBlock.gif
InBlock.gif                csIDInt 
= new DataGridTextBoxColumn();
InBlock.gif                csIDInt.MappingName
="IID";
InBlock.gif                csIDInt.HeaderText
="IID";
InBlock.gif                csIDInt.Width
=100;
InBlock.gif                ts1.GridColumnStyles.Add(csIDInt);
InBlock.gif
InBlock.gif                csIDInt 
= new DataGridTextBoxColumn();
InBlock.gif                csIDInt.MappingName
="Des";
InBlock.gif                csIDInt.HeaderText
="Des";
InBlock.gif                csIDInt.Width
=100;
InBlock.gif                ts1.GridColumnStyles.Add(csIDInt);
InBlock.gif
InBlock.gif                csIDInt 
= new DataGridTextBoxColumn();
InBlock.gif                csIDInt.MappingName
="DataType";
InBlock.gif                csIDInt.HeaderText
="DataType";
InBlock.gif                csIDInt.Width
=50;
InBlock.gif                ts1.GridColumnStyles.Add(csIDInt);
InBlock.gif
InBlock.gif                csIDInt 
= new DataGridTextBoxColumn();
InBlock.gif                csIDInt.MappingName
="TagType";
InBlock.gif                csIDInt.HeaderText
="TagType";
InBlock.gif                csIDInt.Width
=50;
InBlock.gif                ts1.GridColumnStyles.Add(csIDInt);
InBlock.gif
InBlock.gif
InBlock.gif                
//STEP 4: Add the checkbox
InBlock.gif
                DataGridColumnStyle boolCol = new DataGridBoolColumn();
InBlock.gif                boolCol.MappingName 
= "Current";
InBlock.gif                boolCol.HeaderText 
= "请选择要导入的行:";
InBlock.gif                boolCol.Width 
= 100;
InBlock.gif                ts1.GridColumnStyles.Add(boolCol);
InBlock.gif
InBlock.gif
InBlock.gif                
//STEP 5: Add the tablestyle to your datagrid抯 tablestlye collection
InBlock.gif
                this.dataGrid1.TableStyles.Add(ts1);
InBlock.gif                
InBlock.gif                ds.Tables[
"Item"].Rows[0]["Current"]=true;
InBlock.gif                ds.Tables[
"Item"].Rows[1]["Current"]=false;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MessageBox.Show(ex.Message);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void button1_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Form2 form 
= new Form2();
InBlock.gif            form.Show();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void Form1_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void button2_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(flag ==false)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MessageBox.Show(
"还没有选取XML文档!");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
for(int i=0;i<ds.Tables["Item"].Rows.Count;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if(ds.Tables["Item"].Rows[i]["Current"].Equals(true))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
this.richTextBox1.Text+="\n"+ds.Tables["Item"].Rows[i]["TID"].ToString();
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
还有许多不明白的地方,以后将进一步对XML操作的学习。
posted on 2006-05-08 17:30  偷回忆的人 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/liuwenjun830/archive/2006/05/08/394177.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值