由于以前对DataGrid的了解相当初浅,只能用来显示数据
今天做了一下对DataGrid的深入学习
通过在网上搜索相关资料,总算能让例子达到自己的初步要求
也开始明白为什么别人说学习是一个循序渐进的过程
主要用到的一些代码:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Xml;
![](/Images/OutliningIndicators/None.gif)
namespace ReadAndWrite
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button btn_BroswerXML;
private System.Data.DataSet ds;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.DataGridTableStyle dataGridTableStyle1;
private System.Windows.Forms.DataGridBoolColumn dataGridBoolColumn1;
private System.Windows.Forms.Button button2;
![](/Images/OutliningIndicators/InBlock.gif)
private bool flag =false;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
![](/Images/OutliningIndicators/InBlock.gif)
public Form1()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
![](/Images/OutliningIndicators/InBlock.gif)
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if( disposing )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (components != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.panel1 = new System.Windows.Forms.Panel();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.dataGridTableStyle1 = new System.Windows.Forms.DataGridTableStyle();
this.dataGridBoolColumn1 = new System.Windows.Forms.DataGridBoolColumn();
this.ds = new System.Data.DataSet();
this.btn_BroswerXML = new System.Windows.Forms.Button();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.panel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.ds)).BeginInit();
this.SuspendLayout();
//
// panel1
//
this.panel1.Controls.Add(this.dataGrid1);
this.panel1.Dock = System.Windows.Forms.DockStyle.Left;
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(448, 286);
this.panel1.TabIndex = 0;
//
// dataGrid1
//
this.dataGrid1.AlternatingBackColor = System.Drawing.Color.Lavender;
this.dataGrid1.BackColor = System.Drawing.Color.WhiteSmoke;
this.dataGrid1.BackgroundColor = System.Drawing.Color.LightGray;
this.dataGrid1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.dataGrid1.CaptionBackColor = System.Drawing.Color.LightSteelBlue;
this.dataGrid1.CaptionForeColor = System.Drawing.Color.MidnightBlue;
this.dataGrid1.CaptionText = "XML文档相关属性";
this.dataGrid1.DataMember = "";
this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Fill;
this.dataGrid1.FlatMode = true;
this.dataGrid1.Font = new System.Drawing.Font("Tahoma", 8F);
this.dataGrid1.ForeColor = System.Drawing.Color.MidnightBlue;
this.dataGrid1.GridLineColor = System.Drawing.Color.Gainsboro;
this.dataGrid1.GridLineStyle = System.Windows.Forms.DataGridLineStyle.None;
this.dataGrid1.HeaderBackColor = System.Drawing.Color.MidnightBlue;
this.dataGrid1.HeaderFont = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Bold);
this.dataGrid1.HeaderForeColor = System.Drawing.Color.WhiteSmoke;
this.dataGrid1.LinkColor = System.Drawing.Color.Teal;
this.dataGrid1.Location = new System.Drawing.Point(0, 0);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.ParentRowsBackColor = System.Drawing.Color.Gainsboro;
this.dataGrid1.ParentRowsForeColor = System.Drawing.Color.MidnightBlue;
this.dataGrid1.SelectionBackColor = System.Drawing.Color.CadetBlue;
this.dataGrid1.SelectionForeColor = System.Drawing.Color.WhiteSmoke;
this.dataGrid1.Size = new System.Drawing.Size(448, 286);
this.dataGrid1.TabIndex = 1;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.dataGrid1.TableStyles.AddRange(new System.Windows.Forms.DataGridTableStyle[]
{
this.dataGridTableStyle1});
//
// dataGridTableStyle1
//
this.dataGridTableStyle1.DataGrid = this.dataGrid1;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.dataGridTableStyle1.GridColumnStyles.AddRange(new System.Windows.Forms.DataGridColumnStyle[]
{
this.dataGridBoolColumn1});
this.dataGridTableStyle1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGridTableStyle1.MappingName = "";
//
// dataGridBoolColumn1
//
this.dataGridBoolColumn1.FalseValue = false;
this.dataGridBoolColumn1.MappingName = "";
this.dataGridBoolColumn1.NullValue = ((object)(resources.GetObject("dataGridBoolColumn1.NullValue")));
this.dataGridBoolColumn1.TrueValue = true;
this.dataGridBoolColumn1.Width = 75;
//
// ds
//
this.ds.DataSetName = "NewDataSet";
this.ds.Locale = new System.Globalization.CultureInfo("zh-CN");
//
// btn_BroswerXML
//
this.btn_BroswerXML.Location = new System.Drawing.Point(456, 8);
this.btn_BroswerXML.Name = "btn_BroswerXML";
this.btn_BroswerXML.Size = new System.Drawing.Size(96, 23);
this.btn_BroswerXML.TabIndex = 1;
this.btn_BroswerXML.Text = "选择XML文档…";
this.btn_BroswerXML.Click += new System.EventHandler(this.btn_BroswerXML_Click);
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(456, 48);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(104, 144);
this.richTextBox1.TabIndex = 2;
this.richTextBox1.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(472, 200);
this.button1.Name = "button1";
this.button1.TabIndex = 3;
this.button1.Text = "Form2";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(464, 240);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(88, 23);
this.button2.TabIndex = 4;
this.button2.Text = "导入内容预览";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(576, 286);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.btn_BroswerXML);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.panel1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.ds)).EndInit();
this.ResumeLayout(false);
![](/Images/OutliningIndicators/InBlock.gif)
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Application.Run(new Form1());
}
![](/Images/OutliningIndicators/InBlock.gif)
private void btn_BroswerXML_Click(object sender, System.EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory =@"F:\";
openFileDialog.Filter ="XML文件|*.xml";
openFileDialog.RestoreDirectory =true;
openFileDialog.FilterIndex =1;
if(openFileDialog.ShowDialog()==DialogResult.OK)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
flag =true;
ds.ReadXml(openFileDialog.FileName);
this.dataGrid1.DataSource =ds.Tables["Item"];
}
ds.Tables["Item"].Columns.Add("Current",typeof(bool));
//STEP 1: Create a DataTable style object and set properties if required.
DataGridTableStyle ts1 = new DataGridTableStyle();
![](/Images/OutliningIndicators/InBlock.gif)
//specify the table from dataset (required step)
ts1.MappingName = "Item";
// Set other properties (optional step)
ts1.AlternatingBackColor = Color.LightBlue;
![](/Images/OutliningIndicators/InBlock.gif)
//STEP 2: Create a string column and add it to the tablestyle
DataGridColumnStyle TextCol = new DataGridTextBoxColumn();
TextCol.MappingName = "custName"; //from dataset table
TextCol.HeaderText = "Customer Name";
TextCol.Width = 100;
ts1.GridColumnStyles.Add(TextCol);
![](/Images/OutliningIndicators/InBlock.gif)
//STEP 3: Create an int column style and add it to the tablestyle
//this requires setting the format for the column through its property descriptor
PropertyDescriptorCollection pdc = this.BindingContext
[ds, "Item"].GetItemProperties();
![](/Images/OutliningIndicators/InBlock.gif)
//now created a formated column using the pdc
// DataGridTextBoxColumn csIDInt =
// new DataGridTextBoxColumn(pdc["CustID"], "i", true);
// csIDInt.MappingName = "CustID";
// csIDInt.HeaderText = "CustID";
// csIDInt.Width = 50;
// ts1.GridColumnStyles.Add(csIDInt);
DataGridTextBoxColumn csIDInt = new DataGridTextBoxColumn();
csIDInt.MappingName="SID";
csIDInt.HeaderText="SID";
csIDInt.Width=100;
ts1.GridColumnStyles.Add(csIDInt);
csIDInt = new DataGridTextBoxColumn();
csIDInt.MappingName="TID";
csIDInt.HeaderText="TID";
csIDInt.Width=100;
ts1.GridColumnStyles.Add(csIDInt);
![](/Images/OutliningIndicators/InBlock.gif)
csIDInt = new DataGridTextBoxColumn();
csIDInt.MappingName="IID";
csIDInt.HeaderText="IID";
csIDInt.Width=100;
ts1.GridColumnStyles.Add(csIDInt);
![](/Images/OutliningIndicators/InBlock.gif)
csIDInt = new DataGridTextBoxColumn();
csIDInt.MappingName="Des";
csIDInt.HeaderText="Des";
csIDInt.Width=100;
ts1.GridColumnStyles.Add(csIDInt);
![](/Images/OutliningIndicators/InBlock.gif)
csIDInt = new DataGridTextBoxColumn();
csIDInt.MappingName="DataType";
csIDInt.HeaderText="DataType";
csIDInt.Width=50;
ts1.GridColumnStyles.Add(csIDInt);
![](/Images/OutliningIndicators/InBlock.gif)
csIDInt = new DataGridTextBoxColumn();
csIDInt.MappingName="TagType";
csIDInt.HeaderText="TagType";
csIDInt.Width=50;
ts1.GridColumnStyles.Add(csIDInt);
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
//STEP 4: Add the checkbox
DataGridColumnStyle boolCol = new DataGridBoolColumn();
boolCol.MappingName = "Current";
boolCol.HeaderText = "请选择要导入的行:";
boolCol.Width = 100;
ts1.GridColumnStyles.Add(boolCol);
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
//STEP 5: Add the tablestyle to your datagrid抯 tablestlye collection
this.dataGrid1.TableStyles.Add(ts1);
ds.Tables["Item"].Rows[0]["Current"]=true;
ds.Tables["Item"].Rows[1]["Current"]=false;
}
catch(Exception ex)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox.Show(ex.Message);
}
}
![](/Images/OutliningIndicators/InBlock.gif)
private void button1_Click(object sender, System.EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Form2 form = new Form2();
form.Show();
}
![](/Images/OutliningIndicators/InBlock.gif)
private void Form1_Load(object sender, System.EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
![](/Images/OutliningIndicators/InBlock.gif)
private void button2_Click(object sender, System.EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(flag ==false)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox.Show("还没有选取XML文档!");
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for(int i=0;i<ds.Tables["Item"].Rows.Count;i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(ds.Tables["Item"].Rows[i]["Current"].Equals(true))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.richTextBox1.Text+="\n"+ds.Tables["Item"].Rows[i]["TID"].ToString();
}
}
}
}
![](/Images/OutliningIndicators/InBlock.gif)
}
}
![](/Images/OutliningIndicators/None.gif)
还有许多不明白的地方,以后将进一步对XML操作的学习。