1
using
System;
2 using System.Drawing;
3 using System.Collections;
4 using System.Windows.Forms;
5 using System.Data;
6
7 namespace DataGridSample
8 {
9 /**//// <summary>
10 /// Summary description for Form1.
11 /// </summary>
12 public class Form1 : System.Windows.Forms.Form
13 {
14 private System.Windows.Forms.DataGrid dataGrid1;
15 private System.Windows.Forms.MainMenu mainMenu1;
16 private bool TablesAlreadyAdded;
17 private Button button1;
18 private DataSet myDataSet;
19 private DataTable myDataTable;
20
21 public Form1()
22 {
23 //
24 // Required for Windows Form Designer support
25 //
26 InitializeComponent();
27 SetUp();
28 //
29 // TODO: Add any constructor code after InitializeComponent call
30 //
31 }
32 /**//// <summary>
33 /// Clean up any resources being used.
34 /// </summary>
35 protected override void Dispose( bool disposing )
36 {
37 base.Dispose( disposing );
38 }
39 Windows Form Designer generated code#region Windows Form Designer generated code
40 /**//// <summary>
41 /// Required method for Designer support - do not modify
42 /// the contents of this method with the code editor.
43 /// </summary>
44 private void InitializeComponent()
45 {
46 this.mainMenu1 = new System.Windows.Forms.MainMenu();
47 this.dataGrid1 = new System.Windows.Forms.DataGrid();
48 this.button1 = new System.Windows.Forms.Button();
49 //
50 // dataGrid1
51 //
52 this.dataGrid1.Location = new System.Drawing.Point(40, 64);
53 this.dataGrid1.Size = new System.Drawing.Size(168, 168);
54 this.dataGrid1.Text = "dataGrid1";
55 this.dataGrid1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.dataGrid1_MouseUp);
56 //
57 // button1
58 //
59 this.button1.Location = new System.Drawing.Point(32, 16);
60 this.button1.Size = new System.Drawing.Size(72, 24);
61 this.button1.Text = "button1";
62 this.button1.Click += new System.EventHandler(this.button1_Click);
63 //
64 // Form1
65 //
66 this.Controls.Add(this.button1);
67 this.Controls.Add(this.dataGrid1);
68 this.Menu = this.mainMenu1;
69 this.Text = "Form1";
70 this.Load += new System.EventHandler(this.Form1_Load);
71
72 }
73 #endregion
74
75 /**//// <summary>
76 /// The main entry point for the application.
77 /// </summary>
78
79 static void Main()
80 {
81 Application.Run(new Form1());
82 }
83
84 private void Form1_Load(object sender, System.EventArgs e)
85 {
86
87 }
88 private void SetUp()
89 {
90 MakeDataSet();
91 myDataTable = myDataSet.Tables["Customers"];
92 dataGrid1.DataSource = myDataTable;
93 }
94 private void MakeDataSet()
95 {
96 //Create a DataSet
97 myDataSet = new DataSet("myDataSet");
98
99 //Create a DataTable
100 DataTable tCust = new DataTable("Customers");
101 //Create three columns, and add then to the table
102 DataColumn cCustID =new DataColumn("CustID",typeof(int));
103 DataColumn cCustName =new DataColumn("CustName");
104 DataColumn cCurrent =new DataColumn("Current",typeof(bool));
105 tCust.Columns.Add(cCustID);
106 tCust.Columns.Add(cCustName);
107 tCust.Columns.Add(cCurrent);
108
109 //Add the table to the DataSet
110 myDataSet.Tables.Add(tCust);
111
112 //Create three customers in the Customers Table
113 DataRow newRowl;
114 for(int i=1;i<4;i++)
115 {
116 newRowl = tCust.NewRow();
117 newRowl["custID"] = i;
118 tCust.Rows.Add(newRowl);
119 }
120 tCust.Rows[0]["custName"] = "Customer1";
121 tCust.Rows[1]["custName"] = "Customer2";
122 tCust.Rows[2]["custName"] = "Customer3";
123
124 tCust.Rows[0]["Current"] = true;
125 tCust.Rows[1]["Current"] = false;
126 tCust.Rows[2]["Current"] = false;
127 }
128
129 private void button1_Click(object sender, System.EventArgs e)
130 {
131 if(TablesAlreadyAdded)
132 return;
133 AddCustomDataTableStyle();
134 }
135 private void AddCustomDataTableStyle()
136 {
137 DataGridTableStyle tsl =new DataGridTableStyle();
138 tsl.MappingName = "Customer";
139
140 DataGridColumnStyle boolCol =new DataGridTextBoxColumn();
141 boolCol.MappingName = "Current";
142 boolCol.HeaderText = "IsCurrent Customer";
143 boolCol.Width = 80;
144 tsl.GridColumnStyles.Add(boolCol);
145 dataGrid1.TableStyles.Add(tsl);
146 TablesAlreadyAdded = true;
147 }
148
149 private void dataGrid1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
150 {
151 DataGrid myGrid = (DataGrid)sender;
152 DataGrid.HitTestInfo myHitInfo = myGrid.HitTest(e.X,e.Y);
153 Console.WriteLine(myHitInfo);
154 Console.WriteLine(myHitInfo.Type);
155 Console.WriteLine(myHitInfo.Row);
156 Console.WriteLine(myHitInfo.Column);
157
158 if(myHitInfo.Type == DataGrid.HitTestType.Cell)
159 {
160 Console.Write("Data: ");
161 object celldata =myGrid[myHitInfo.Row,myHitInfo.Column];
162 MessageBox.Show(celldata.ToString());
163 }
164 }
165 }
166}
167
2 using System.Drawing;
3 using System.Collections;
4 using System.Windows.Forms;
5 using System.Data;
6
7 namespace DataGridSample
8 {
9 /**//// <summary>
10 /// Summary description for Form1.
11 /// </summary>
12 public class Form1 : System.Windows.Forms.Form
13 {
14 private System.Windows.Forms.DataGrid dataGrid1;
15 private System.Windows.Forms.MainMenu mainMenu1;
16 private bool TablesAlreadyAdded;
17 private Button button1;
18 private DataSet myDataSet;
19 private DataTable myDataTable;
20
21 public Form1()
22 {
23 //
24 // Required for Windows Form Designer support
25 //
26 InitializeComponent();
27 SetUp();
28 //
29 // TODO: Add any constructor code after InitializeComponent call
30 //
31 }
32 /**//// <summary>
33 /// Clean up any resources being used.
34 /// </summary>
35 protected override void Dispose( bool disposing )
36 {
37 base.Dispose( disposing );
38 }
39 Windows Form Designer generated code#region Windows Form Designer generated code
40 /**//// <summary>
41 /// Required method for Designer support - do not modify
42 /// the contents of this method with the code editor.
43 /// </summary>
44 private void InitializeComponent()
45 {
46 this.mainMenu1 = new System.Windows.Forms.MainMenu();
47 this.dataGrid1 = new System.Windows.Forms.DataGrid();
48 this.button1 = new System.Windows.Forms.Button();
49 //
50 // dataGrid1
51 //
52 this.dataGrid1.Location = new System.Drawing.Point(40, 64);
53 this.dataGrid1.Size = new System.Drawing.Size(168, 168);
54 this.dataGrid1.Text = "dataGrid1";
55 this.dataGrid1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.dataGrid1_MouseUp);
56 //
57 // button1
58 //
59 this.button1.Location = new System.Drawing.Point(32, 16);
60 this.button1.Size = new System.Drawing.Size(72, 24);
61 this.button1.Text = "button1";
62 this.button1.Click += new System.EventHandler(this.button1_Click);
63 //
64 // Form1
65 //
66 this.Controls.Add(this.button1);
67 this.Controls.Add(this.dataGrid1);
68 this.Menu = this.mainMenu1;
69 this.Text = "Form1";
70 this.Load += new System.EventHandler(this.Form1_Load);
71
72 }
73 #endregion
74
75 /**//// <summary>
76 /// The main entry point for the application.
77 /// </summary>
78
79 static void Main()
80 {
81 Application.Run(new Form1());
82 }
83
84 private void Form1_Load(object sender, System.EventArgs e)
85 {
86
87 }
88 private void SetUp()
89 {
90 MakeDataSet();
91 myDataTable = myDataSet.Tables["Customers"];
92 dataGrid1.DataSource = myDataTable;
93 }
94 private void MakeDataSet()
95 {
96 //Create a DataSet
97 myDataSet = new DataSet("myDataSet");
98
99 //Create a DataTable
100 DataTable tCust = new DataTable("Customers");
101 //Create three columns, and add then to the table
102 DataColumn cCustID =new DataColumn("CustID",typeof(int));
103 DataColumn cCustName =new DataColumn("CustName");
104 DataColumn cCurrent =new DataColumn("Current",typeof(bool));
105 tCust.Columns.Add(cCustID);
106 tCust.Columns.Add(cCustName);
107 tCust.Columns.Add(cCurrent);
108
109 //Add the table to the DataSet
110 myDataSet.Tables.Add(tCust);
111
112 //Create three customers in the Customers Table
113 DataRow newRowl;
114 for(int i=1;i<4;i++)
115 {
116 newRowl = tCust.NewRow();
117 newRowl["custID"] = i;
118 tCust.Rows.Add(newRowl);
119 }
120 tCust.Rows[0]["custName"] = "Customer1";
121 tCust.Rows[1]["custName"] = "Customer2";
122 tCust.Rows[2]["custName"] = "Customer3";
123
124 tCust.Rows[0]["Current"] = true;
125 tCust.Rows[1]["Current"] = false;
126 tCust.Rows[2]["Current"] = false;
127 }
128
129 private void button1_Click(object sender, System.EventArgs e)
130 {
131 if(TablesAlreadyAdded)
132 return;
133 AddCustomDataTableStyle();
134 }
135 private void AddCustomDataTableStyle()
136 {
137 DataGridTableStyle tsl =new DataGridTableStyle();
138 tsl.MappingName = "Customer";
139
140 DataGridColumnStyle boolCol =new DataGridTextBoxColumn();
141 boolCol.MappingName = "Current";
142 boolCol.HeaderText = "IsCurrent Customer";
143 boolCol.Width = 80;
144 tsl.GridColumnStyles.Add(boolCol);
145 dataGrid1.TableStyles.Add(tsl);
146 TablesAlreadyAdded = true;
147 }
148
149 private void dataGrid1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
150 {
151 DataGrid myGrid = (DataGrid)sender;
152 DataGrid.HitTestInfo myHitInfo = myGrid.HitTest(e.X,e.Y);
153 Console.WriteLine(myHitInfo);
154 Console.WriteLine(myHitInfo.Type);
155 Console.WriteLine(myHitInfo.Row);
156 Console.WriteLine(myHitInfo.Column);
157
158 if(myHitInfo.Type == DataGrid.HitTestType.Cell)
159 {
160 Console.Write("Data: ");
161 object celldata =myGrid[myHitInfo.Row,myHitInfo.Column];
162 MessageBox.Show(celldata.ToString());
163 }
164 }
165 }
166}
167