zoukankan      html  css  js  c++  java
  • 对DataGrid的初步了解

    由于以前对DataGrid的了解相当初浅,只能用来显示数据
    今天做了一下对DataGrid的深入学习
    通过在网上搜索相关资料,总算能让例子达到自己的初步要求
    也开始明白为什么别人说学习是一个循序渐进的过程
    主要用到的一些代码:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Xml;

    namespace ReadAndWrite
    {
        
    /// <summary>
        
    /// Form1 的摘要说明。
        
    /// </summary>

        public class Form1 : System.Windows.Forms.Form
        
    {
            
    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;

            
    private bool flag =false;
            
    /// <summary>
            
    /// 必需的设计器变量。
            
    /// </summary>

            private System.ComponentModel.Container components = null;

            
    public Form1()
            
    {
                
    //
                
    // Windows 窗体设计器支持所必需的
                
    //
                InitializeComponent();

                
    //
                
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
                
    //
            }


            
    /// <summary>
            
    /// 清理所有正在使用的资源。
            
    /// </summary>

            protected override void Dispose( bool disposing )
            
    {
                
    if( disposing )
                
    {
                    
    if (components != null
                    
    {
                        components.Dispose();
                    }

                }

                
    base.Dispose( disposing );
            }


            
    Windows 窗体设计器生成的代码

            
    /// <summary>
            
    /// 应用程序的主入口点。
            
    /// </summary>

            [STAThread]
            
    static void Main() 
            
    {
                Application.Run(
    new Form1());
            }


            
    private void btn_BroswerXML_Click(object sender, System.EventArgs e)
            
    {
                
    try
                
    {
                    OpenFileDialog openFileDialog 
    = new OpenFileDialog();
                    openFileDialog.InitialDirectory 
    =@"F:\";
                    openFileDialog.Filter 
    ="XML文件|*.xml";
                    openFileDialog.RestoreDirectory 
    =true;
                    openFileDialog.FilterIndex 
    =1;
                    
    if(openFileDialog.ShowDialog()==DialogResult.OK)
                    
    {
                        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();

                    
    //specify the table from dataset (required step)
                    ts1.MappingName = "Item";
              
                    
    // Set other properties (optional step)
                    ts1.AlternatingBackColor = Color.LightBlue;

                    
    //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);

                    
    //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();

                    
    //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);

                    csIDInt 
    = new DataGridTextBoxColumn();
                    csIDInt.MappingName
    ="IID";
                    csIDInt.HeaderText
    ="IID";
                    csIDInt.Width
    =100;
                    ts1.GridColumnStyles.Add(csIDInt);

                    csIDInt 
    = new DataGridTextBoxColumn();
                    csIDInt.MappingName
    ="Des";
                    csIDInt.HeaderText
    ="Des";
                    csIDInt.Width
    =100;
                    ts1.GridColumnStyles.Add(csIDInt);

                    csIDInt 
    = new DataGridTextBoxColumn();
                    csIDInt.MappingName
    ="DataType";
                    csIDInt.HeaderText
    ="DataType";
                    csIDInt.Width
    =50;
                    ts1.GridColumnStyles.Add(csIDInt);

                    csIDInt 
    = new DataGridTextBoxColumn();
                    csIDInt.MappingName
    ="TagType";
                    csIDInt.HeaderText
    ="TagType";
                    csIDInt.Width
    =50;
                    ts1.GridColumnStyles.Add(csIDInt);


                    
    //STEP 4: Add the checkbox
                    DataGridColumnStyle boolCol = new DataGridBoolColumn();
                    boolCol.MappingName 
    = "Current";
                    boolCol.HeaderText 
    = "请选择要导入的行:";
                    boolCol.Width 
    = 100;
                    ts1.GridColumnStyles.Add(boolCol);


                    
    //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)
                
    {
                    MessageBox.Show(ex.Message);
                }

            }


            
    private void button1_Click(object sender, System.EventArgs e)
            
    {
                Form2 form 
    = new Form2();
                form.Show();
            }


            
    private void Form1_Load(object sender, System.EventArgs e)
            
    {
                
            }


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

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

                    }

                }

            }


        }

    }

    还有许多不明白的地方,以后将进一步对XML操作的学习。
  • 相关阅读:
    [转]C++中cin、cin.get()、cin.getline()、getline()函数的简单总结
    Assert 的用法
    [转]C/C++作用域详解
    C++ 的getline问题
    字符数组的定义与赋值
    [转] 字符数组的赋值
    [转]标准C++中的string类的用法总结
    [转]memmove、memcpy和memccpy
    关于变长数组的一点小想法-C语言定义数组但是数组长度不确定怎么办
    Java动态代理演变之路
  • 原文地址:https://www.cnblogs.com/liuwenjun830/p/394177.html
Copyright © 2011-2022 走看看