Controls in .Net Framework 之 DataGrid Control

时间:2022-07-21 16:02:46

The DataGrid Control

  1 Controls in .Net Framework 之 DataGrid Controlusing  System;
  2 Controls in .Net Framework 之 DataGrid Control using  System.Drawing;
  3 Controls in .Net Framework 之 DataGrid Control using  System.Collections;
  4 Controls in .Net Framework 之 DataGrid Control using  System.Windows.Forms;
  5 Controls in .Net Framework 之 DataGrid Control using  System.Data;
  6 Controls in .Net Framework 之 DataGrid Control
  7 Controls in .Net Framework 之 DataGrid Control namespace  DataGridSample
  8 Controls in .Net Framework 之 DataGrid ControlControls in .Net Framework 之 DataGrid Control {
  9Controls in .Net Framework 之 DataGrid ControlControls in .Net Framework 之 DataGrid Control    /// <summary>
 10Controls in .Net Framework 之 DataGrid Control    /// Summary description for Form1.
 11Controls in .Net Framework 之 DataGrid Control    /// </summary>

 12Controls in .Net Framework 之 DataGrid Control    public class Form1 : System.Windows.Forms.Form
 13Controls in .Net Framework 之 DataGrid ControlControls in .Net Framework 之 DataGrid Control    {
 14Controls in .Net Framework 之 DataGrid Control        private System.Windows.Forms.DataGrid dataGrid1;
 15Controls in .Net Framework 之 DataGrid Control        private System.Windows.Forms.MainMenu mainMenu1;
 16Controls in .Net Framework 之 DataGrid Control        private bool TablesAlreadyAdded;
 17Controls in .Net Framework 之 DataGrid Control        private Button button1;
 18Controls in .Net Framework 之 DataGrid Control        private DataSet    myDataSet;
 19Controls in .Net Framework 之 DataGrid Control        private DataTable    myDataTable;
 20Controls in .Net Framework 之 DataGrid Control
 21Controls in .Net Framework 之 DataGrid Control        public Form1()
 22Controls in .Net Framework 之 DataGrid ControlControls in .Net Framework 之 DataGrid Control        {
 23Controls in .Net Framework 之 DataGrid Control            //
 24Controls in .Net Framework 之 DataGrid Control            // Required for Windows Form Designer support
 25Controls in .Net Framework 之 DataGrid Control            //
 26Controls in .Net Framework 之 DataGrid Control            InitializeComponent();
 27Controls in .Net Framework 之 DataGrid Control            SetUp();
 28Controls in .Net Framework 之 DataGrid Control            //
 29Controls in .Net Framework 之 DataGrid Control            // TODO: Add any constructor code after InitializeComponent call
 30Controls in .Net Framework 之 DataGrid Control            //
 31Controls in .Net Framework 之 DataGrid Control        }

 32Controls in .Net Framework 之 DataGrid ControlControls in .Net Framework 之 DataGrid Control        /// <summary>
 33Controls in .Net Framework 之 DataGrid Control        /// Clean up any resources being used.
 34Controls in .Net Framework 之 DataGrid Control        /// </summary>

 35Controls in .Net Framework 之 DataGrid Control        protected override void Dispose( bool disposing )
 36Controls in .Net Framework 之 DataGrid ControlControls in .Net Framework 之 DataGrid Control        {
 37Controls in .Net Framework 之 DataGrid Control            base.Dispose( disposing );
 38Controls in .Net Framework 之 DataGrid Control        }

 39Controls in .Net Framework 之 DataGrid ControlControls in .Net Framework 之 DataGrid Control        #region Windows Form Designer generated code
 40Controls in .Net Framework 之 DataGrid ControlControls in .Net Framework 之 DataGrid Control        /// <summary>
 41Controls in .Net Framework 之 DataGrid Control        /// Required method for Designer support - do not modify
 42Controls in .Net Framework 之 DataGrid Control        /// the contents of this method with the code editor.
 43Controls in .Net Framework 之 DataGrid Control        /// </summary>

 44Controls in .Net Framework 之 DataGrid Control        private void InitializeComponent()
 45Controls in .Net Framework 之 DataGrid ControlControls in .Net Framework 之 DataGrid Control        {
 46Controls in .Net Framework 之 DataGrid Control            this.mainMenu1 = new System.Windows.Forms.MainMenu();
 47Controls in .Net Framework 之 DataGrid Control            this.dataGrid1 = new System.Windows.Forms.DataGrid();
 48Controls in .Net Framework 之 DataGrid Control            this.button1 = new System.Windows.Forms.Button();
 49Controls in .Net Framework 之 DataGrid Control            // 
 50Controls in .Net Framework 之 DataGrid Control            // dataGrid1
 51Controls in .Net Framework 之 DataGrid Control            // 
 52Controls in .Net Framework 之 DataGrid Control            this.dataGrid1.Location = new System.Drawing.Point(4064);
 53Controls in .Net Framework 之 DataGrid Control            this.dataGrid1.Size = new System.Drawing.Size(168168);
 54Controls in .Net Framework 之 DataGrid Control            this.dataGrid1.Text = "dataGrid1";
 55Controls in .Net Framework 之 DataGrid Control            this.dataGrid1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.dataGrid1_MouseUp);
 56Controls in .Net Framework 之 DataGrid Control            // 
 57Controls in .Net Framework 之 DataGrid Control            // button1
 58Controls in .Net Framework 之 DataGrid Control            // 
 59Controls in .Net Framework 之 DataGrid Control            this.button1.Location = new System.Drawing.Point(3216);
 60Controls in .Net Framework 之 DataGrid Control            this.button1.Size = new System.Drawing.Size(7224);
 61Controls in .Net Framework 之 DataGrid Control            this.button1.Text = "button1";
 62Controls in .Net Framework 之 DataGrid Control            this.button1.Click += new System.EventHandler(this.button1_Click);
 63Controls in .Net Framework 之 DataGrid Control            // 
 64Controls in .Net Framework 之 DataGrid Control            // Form1
 65Controls in .Net Framework 之 DataGrid Control            // 
 66Controls in .Net Framework 之 DataGrid Control            this.Controls.Add(this.button1);
 67Controls in .Net Framework 之 DataGrid Control            this.Controls.Add(this.dataGrid1);
 68Controls in .Net Framework 之 DataGrid Control            this.Menu = this.mainMenu1;
 69Controls in .Net Framework 之 DataGrid Control            this.Text = "Form1";
 70Controls in .Net Framework 之 DataGrid Control            this.Load += new System.EventHandler(this.Form1_Load);
 71Controls in .Net Framework 之 DataGrid Control
 72Controls in .Net Framework 之 DataGrid Control        }

 73Controls in .Net Framework 之 DataGrid Control        #endregion

 74Controls in .Net Framework 之 DataGrid Control
 75Controls in .Net Framework 之 DataGrid ControlControls in .Net Framework 之 DataGrid Control        /// <summary>
 76Controls in .Net Framework 之 DataGrid Control        /// The main entry point for the application.
 77Controls in .Net Framework 之 DataGrid Control        /// </summary>

 78Controls in .Net Framework 之 DataGrid Control
 79Controls in .Net Framework 之 DataGrid Control        static void Main() 
 80Controls in .Net Framework 之 DataGrid ControlControls in .Net Framework 之 DataGrid Control        {
 81Controls in .Net Framework 之 DataGrid Control            Application.Run(new Form1());
 82Controls in .Net Framework 之 DataGrid Control        }

 83Controls in .Net Framework 之 DataGrid Control
 84Controls in .Net Framework 之 DataGrid Control        private void Form1_Load(object sender, System.EventArgs e)
 85Controls in .Net Framework 之 DataGrid ControlControls in .Net Framework 之 DataGrid Control        {
 86Controls in .Net Framework 之 DataGrid Control        
 87Controls in .Net Framework 之 DataGrid Control        }

 88Controls in .Net Framework 之 DataGrid Control        private void SetUp()
 89Controls in .Net Framework 之 DataGrid ControlControls in .Net Framework 之 DataGrid Control        {
 90Controls in .Net Framework 之 DataGrid Control            MakeDataSet();
 91Controls in .Net Framework 之 DataGrid Control            myDataTable = myDataSet.Tables["Customers"];
 92Controls in .Net Framework 之 DataGrid Control            dataGrid1.DataSource = myDataTable;
 93Controls in .Net Framework 之 DataGrid Control        }

 94Controls in .Net Framework 之 DataGrid Control        private void MakeDataSet()
 95Controls in .Net Framework 之 DataGrid ControlControls in .Net Framework 之 DataGrid Control        {
 96Controls in .Net Framework 之 DataGrid Control            //Create a DataSet
 97Controls in .Net Framework 之 DataGrid Control            myDataSet = new DataSet("myDataSet");
 98Controls in .Net Framework 之 DataGrid Control
 99Controls in .Net Framework 之 DataGrid Control            //Create a DataTable
100Controls in .Net Framework 之 DataGrid Control            DataTable tCust = new DataTable("Customers");
101Controls in .Net Framework 之 DataGrid Control            //Create three columns, and add then to the table
102Controls in .Net Framework 之 DataGrid Control            DataColumn cCustID =new DataColumn("CustID",typeof(int));
103Controls in .Net Framework 之 DataGrid Control            DataColumn cCustName =new DataColumn("CustName");
104Controls in .Net Framework 之 DataGrid Control            DataColumn cCurrent =new DataColumn("Current",typeof(bool));
105Controls in .Net Framework 之 DataGrid Control            tCust.Columns.Add(cCustID);
106Controls in .Net Framework 之 DataGrid Control            tCust.Columns.Add(cCustName);
107Controls in .Net Framework 之 DataGrid Control            tCust.Columns.Add(cCurrent);
108Controls in .Net Framework 之 DataGrid Control
109Controls in .Net Framework 之 DataGrid Control            //Add the table to the DataSet
110Controls in .Net Framework 之 DataGrid Control            myDataSet.Tables.Add(tCust);
111Controls in .Net Framework 之 DataGrid Control
112Controls in .Net Framework 之 DataGrid Control            //Create three customers in the Customers Table
113Controls in .Net Framework 之 DataGrid Control            DataRow newRowl;
114Controls in .Net Framework 之 DataGrid Control            for(int i=1;i<4;i++)
115Controls in .Net Framework 之 DataGrid ControlControls in .Net Framework 之 DataGrid Control            {
116Controls in .Net Framework 之 DataGrid Control                newRowl = tCust.NewRow();
117Controls in .Net Framework 之 DataGrid Control                newRowl["custID"= i;
118Controls in .Net Framework 之 DataGrid Control                tCust.Rows.Add(newRowl);
119Controls in .Net Framework 之 DataGrid Control            }

120Controls in .Net Framework 之 DataGrid Control            tCust.Rows[0]["custName"= "Customer1";
121Controls in .Net Framework 之 DataGrid Control            tCust.Rows[1]["custName"= "Customer2";
122Controls in .Net Framework 之 DataGrid Control            tCust.Rows[2]["custName"= "Customer3";
123Controls in .Net Framework 之 DataGrid Control
124Controls in .Net Framework 之 DataGrid Control            tCust.Rows[0]["Current"= true;
125Controls in .Net Framework 之 DataGrid Control            tCust.Rows[1]["Current"= false;
126Controls in .Net Framework 之 DataGrid Control            tCust.Rows[2]["Current"= false;
127Controls in .Net Framework 之 DataGrid Control        }

128Controls in .Net Framework 之 DataGrid Control
129Controls in .Net Framework 之 DataGrid Control        private void button1_Click(object sender, System.EventArgs e)
130Controls in .Net Framework 之 DataGrid ControlControls in .Net Framework 之 DataGrid Control        {
131Controls in .Net Framework 之 DataGrid Control            if(TablesAlreadyAdded) 
132Controls in .Net Framework 之 DataGrid Control                return;
133Controls in .Net Framework 之 DataGrid Control            AddCustomDataTableStyle();
134Controls in .Net Framework 之 DataGrid Control        }

135Controls in .Net Framework 之 DataGrid Control        private void AddCustomDataTableStyle()
136Controls in .Net Framework 之 DataGrid ControlControls in .Net Framework 之 DataGrid Control        {
137Controls in .Net Framework 之 DataGrid Control            DataGridTableStyle tsl =new DataGridTableStyle();
138Controls in .Net Framework 之 DataGrid Control            tsl.MappingName = "Customer";
139Controls in .Net Framework 之 DataGrid Control            
140Controls in .Net Framework 之 DataGrid Control            DataGridColumnStyle boolCol =new DataGridTextBoxColumn();
141Controls in .Net Framework 之 DataGrid Control            boolCol.MappingName = "Current";
142Controls in .Net Framework 之 DataGrid Control            boolCol.HeaderText = "IsCurrent Customer";
143Controls in .Net Framework 之 DataGrid Control            boolCol.Width = 80;
144Controls in .Net Framework 之 DataGrid Control            tsl.GridColumnStyles.Add(boolCol);
145Controls in .Net Framework 之 DataGrid Control            dataGrid1.TableStyles.Add(tsl);
146Controls in .Net Framework 之 DataGrid Control            TablesAlreadyAdded = true;
147Controls in .Net Framework 之 DataGrid Control        }

148Controls in .Net Framework 之 DataGrid Control
149Controls in .Net Framework 之 DataGrid Control        private void dataGrid1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
150Controls in .Net Framework 之 DataGrid ControlControls in .Net Framework 之 DataGrid Control        {
151Controls in .Net Framework 之 DataGrid Control            DataGrid    myGrid = (DataGrid)sender;
152Controls in .Net Framework 之 DataGrid Control            DataGrid.HitTestInfo myHitInfo = myGrid.HitTest(e.X,e.Y);
153Controls in .Net Framework 之 DataGrid Control            Console.WriteLine(myHitInfo);
154Controls in .Net Framework 之 DataGrid Control            Console.WriteLine(myHitInfo.Type);
155Controls in .Net Framework 之 DataGrid Control            Console.WriteLine(myHitInfo.Row);
156Controls in .Net Framework 之 DataGrid Control            Console.WriteLine(myHitInfo.Column);
157Controls in .Net Framework 之 DataGrid Control
158Controls in .Net Framework 之 DataGrid Control            if(myHitInfo.Type == DataGrid.HitTestType.Cell)
159Controls in .Net Framework 之 DataGrid ControlControls in .Net Framework 之 DataGrid Control            {
160Controls in .Net Framework 之 DataGrid Control                Console.Write("Data: ");
161Controls in .Net Framework 之 DataGrid Control                object celldata =myGrid[myHitInfo.Row,myHitInfo.Column];
162Controls in .Net Framework 之 DataGrid Control                MessageBox.Show(celldata.ToString());
163Controls in .Net Framework 之 DataGrid Control            }

164Controls in .Net Framework 之 DataGrid Control        }

165Controls in .Net Framework 之 DataGrid Control    }

166Controls in .Net Framework 之 DataGrid Control}

167 Controls in .Net Framework 之 DataGrid Control