|
|
|
 |
 |
Исходник |
 |
|
 |
 |
|
Автор:
|
|
|
Название:
|
DataGridComboBoxColumn Part II (Samle app) |
|
Дата:
|
07 October 2002 |
|
Описание: |
Продолжение первого постинга посвещенного соданию сложных контролах в ячейках стандартного грида. |
| |
Разместить ссылку на этот исходник в форуме вы можете вставив в текст сообщения
следующую строку:
[CODEPOST ID=9]DataGridComboBoxColumn Part II (Samle app)[/CODEPOST] |
| Оценка: |
Проголосовало 2 посетителей, средняя оценка 3.00 |
| Оценить: |
|
1 using System;
2 using System.Drawing;
3 using System.Collections;
4 using System.ComponentModel;
5 using System.Windows.Forms;
6 using System.Data;
7
8 namespace DataGridComboboxColumnSample
9 {
10 /// <summary>
11 /// Summary description for Form1.
12 /// </summary>
13 public class Form1 : System.Windows.Forms.Form
14 {
15 private DataGridComboboxColumnSample.CustomersDataSet customersDataSet1;
16 private System.Windows.Forms.DataGrid dataGrid1;
17 private System.Windows.Forms.Button button1;
18 /// <summary>
19 /// Required designer variable.
20 /// </summary>
21 private System.ComponentModel.Container components = null;
22 private System.Windows.Forms.Button button2;
23 private System.Windows.Forms.DataGridTableStyle dataGridTableStyle1;
24 private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn1;
25 private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn2;
26 private System.Data.SqlClient.SqlCommand sqlSelectCommand1;
27 private System.Data.SqlClient.SqlCommand sqlInsertCommand1;
28 private System.Data.SqlClient.SqlCommand sqlUpdateCommand1;
29 private System.Data.SqlClient.SqlCommand sqlDeleteCommand1;
30 private System.Data.SqlClient.SqlConnection sqlConnection1;
31 private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
32
33 //Define the array of states for the drop-down list
34 public State[] States = new State[] {
35 new State("Alabama","AL")
36 ,new State("Alaska","AK")
37 ,new State("Arizona" ,"AZ")
38 ,new State("Arkansas","AR")
39 ,new State("California" ,"CA")
40 ,new State("Colorado","CO")
41 ,new State("Connecticut","CT")
42 ,new State("Delaware","DE")
43 ,new State("District of Columbia","DC")
44 ,new State("Florida" ,"FL")
45 ,new State("Georgia" ,"GA")
46 ,new State("Hawaii" ,"HI")
47 ,new State("Idaho","ID")
48 ,new State("Illinois","IL")
49 ,new State("Indiana" ,"IN")
50 ,new State("Iowa","IA")
51 ,new State("Kansas" ,"KS")
52 ,new State("Kentucky","KY")
53 ,new State("Louisiana" ,"LA")
54 ,new State("Maine","ME")
55 ,new State("Maryland","MD")
56 ,new State("Massachusetts","MA")
57 ,new State("Michigan","MI")
58 ,new State("Minnesota" ,"MN")
59 ,new State("Mississippi","MS")
60 ,new State("Missouri","MO")
61 ,new State("Montana" ,"MT")
62 ,new State("Nebraska","NE")
63 ,new State("Nevada" ,"NV")
64 ,new State("New Hampshire","NH")
65 ,new State("New Jersey" ,"NJ")
66 ,new State("New Mexico" ,"NM")
67 ,new State("New York","NY")
68 ,new State("North Carolina","NC")
69 ,new State("North Dakota" ,"ND")
70 ,new State("Ohio","OH")
71 ,new State("Oklahoma","OK")
72 ,new State("Oregon" ,"OR")
73 ,new State("Pennsylvania" ,"PA")
74 ,new State("Rhode Island" ,"RI")
75 ,new State("South Carolina","SC")
76 ,new State("South Dakota" ,"SD")
77 ,new State("Tennessee" ,"TN")
78 ,new State("Texas","TX")
79 ,new State("Utah","UT")
80 ,new State("Vermont" ,"VT")
81 ,new State("Virginia","VA")
82 ,new State("Washington" ,"WA")
83 ,new State("West Virginia","WV")
84 ,new State("Wisconsin" ,"WI")
85 ,new State("Wyoming" ,"WY")
86 } ;
87
88
89 public Form1()
90 {
91 //
92 // Required for Windows Form Designer support
93 //
94 InitializeComponent();
95
96 //
97 // TODO: Add any constructor code after InitializeComponent call
98 //
99 DataGridComboBoxColumnStyle col1 = new DataGridComboBoxColumnStyle();
100 col1.MappingName = "Region";
101 col1.HeaderText = "State";
102 col1.DataSource = States; //This could just as easily be a datatable in a dataset
103 col1.DisplayMember = "LongName";
104 col1.ValueMember = "ShortName";
105 dataGridTableStyle1.GridColumnStyles.Add(col1);
106
107 DataGridTextBoxColumn col2 = new DataGridTextBoxColumn();
108 col2.MappingName = "Country";
109 col2.HeaderText = "Country";
110 dataGridTableStyle1.GridColumnStyles.Add(col2);
111
112 }
113
114 /// <summary>
115 /// Clean up any resources being used.
116 /// </summary>
117 protected override void Dispose( bool disposing )
118 {
119 if( disposing )
120 {
121 if (components != null)
122 {
123 components.Dispose();
124 }
125 }
126 base.Dispose( disposing );
127 }
128
129 #region Windows Form Designer generated code
130 /// <summary>
131 /// Required method for Designer support - do not modify
132 /// the contents of this method with the code editor.
133 /// </summary>
134 private void InitializeComponent()
135 {
136 this.dataGrid1 = new System.Windows.Forms.DataGrid();
137 this.customersDataSet1 = new DataGridComboboxColumnSample.CustomersDataSet();
138 this.dataGridTableStyle1 = new System.Windows.Forms.DataGridTableStyle();
139 this.dataGridTextBoxColumn1 = new System.Windows.Forms.DataGridTextBoxColumn();
140 this.dataGridTextBoxColumn2 = new System.Windows.Forms.DataGridTextBoxColumn();
141 this.button2 = new System.Windows.Forms.Button();
142 this.button1 = new System.Windows.Forms.Button();
143 this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
144 this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
145 this.sqlUpdateCommand1 = new System.Data.SqlClient.SqlCommand();
146 this.sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand();
147 this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
148 this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
149 ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
150 ((System.ComponentModel.ISupportInitialize)(this.customersDataSet1)).BeginInit();
151 this.SuspendLayout();
152 //
153 // dataGrid1
154 //
155 this.dataGrid1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
156 | System.Windows.Forms.AnchorStyles.Left)
157 | System.Windows.Forms.AnchorStyles.Right);
158 this.dataGrid1.DataMember = "Customers";
159 this.dataGrid1.DataSource = this.customersDataSet1;
160 this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
161 this.dataGrid1.Name = "dataGrid1";
162 this.dataGrid1.Size = new System.Drawing.Size(460, 216);
163 this.dataGrid1.TabIndex = 0;
164 this.dataGrid1.TableStyles.AddRange(new System.Windows.Forms.DataGridTableStyle[] {
165 this.dataGridTableStyle1});
166 //
167 // customersDataSet1
168 //
169 this.customersDataSet1.DataSetName = "CustomersDataSet";
170 this.customersDataSet1.Locale = new System.Globalization.CultureInfo("en-US");
171 this.customersDataSet1.Namespace = "http://www.tempuri.org/CustomersDataSet.xsd";
172 //
173 // dataGridTableStyle1
174 //
175 this.dataGridTableStyle1.DataGrid = this.dataGrid1;
176 this.dataGridTableStyle1.GridColumnStyles.AddRange(new System.Windows.Forms.DataGridColumnStyle[] {
177 this.dataGridTextBoxColumn1,
178 this.dataGridTextBoxColumn2});
179 this.dataGridTableStyle1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
180 this.dataGridTableStyle1.MappingName = "Customers";
181 //
182 // dataGridTextBoxColumn1
183 //
184 this.dataGridTextBoxColumn1.Format = "";
185 this.dataGridTextBoxColumn1.FormatInfo = null;
186 this.dataGridTextBoxColumn1.HeaderText = "ID";
187 this.dataGridTextBoxColumn1.MappingName = "CustomerID";
188 this.dataGridTextBoxColumn1.Width = 75;
189 //
190 // dataGridTextBoxColumn2
191 //
192 this.dataGridTextBoxColumn2.Format = "";
193 this.dataGridTextBoxColumn2.FormatInfo = null;
194 this.dataGridTextBoxColumn2.HeaderText = "Company Name";
195 this.dataGridTextBoxColumn2.MappingName = "CompanyName";
196 this.dataGridTextBoxColumn2.Width = 75;
197 //
198 // button2
199 //
200 this.button2.Location = new System.Drawing.Point(128, 240);
201 this.button2.Name = "button2";
202 this.button2.Size = new System.Drawing.Size(104, 32);
203 this.button2.TabIndex = 2;
204 this.button2.Text = "Get Changes";
205 this.button2.Click += new System.EventHandler(this.button2_Click);
206 //
207 // button1
208 //
209 this.button1.Location = new System.Drawing.Point(8, 240);
210 this.button1.Name = "button1";
211 this.button1.Size = new System.Drawing.Size(104, 32);
212 this.button1.TabIndex = 1;
213 this.button1.Text = "Load";
214 this.button1.Click += new System.EventHandler(this.button1_Click);
215 //
216 // sqlSelectCommand1
217 //
218 this.sqlSelectCommand1.CommandText = "SELECT CategoryID, CategoryName, Description, Picture FROM Categories";
219 this.sqlSelectCommand1.Connection = this.sqlConnection1;
220 //
221 // sqlInsertCommand1
222 //
223 this.sqlInsertCommand1.CommandText = "INSERT INTO Categories(CategoryName, Description, Picture) VALUES (@CategoryName," +
224 " @Description, @Picture); SELECT CategoryID, CategoryName, Description, Picture " +
225 "FROM Categories WHERE (CategoryID = @@IDENTITY)";
226 this.sqlInsertCommand1.Connection = this.sqlConnection1;
227 this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CategoryName", System.Data.SqlDbType.NVarChar, 15, "CategoryName"));
228 this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Description", System.Data.SqlDbType.NVarChar, 1073741823, "Description"));
229 this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Picture", System.Data.SqlDbType.VarBinary, 2147483647, "Picture"));
230 //
231 // sqlUpdateCommand1
232 //
233 this.sqlUpdateCommand1.CommandText = @"UPDATE Categories SET CategoryName = @CategoryName, Description = @Description, Picture = @Picture WHERE (CategoryID = @Original_CategoryID) AND (CategoryName = @Original_CategoryName); SELECT CategoryID, CategoryName, Description, Picture FROM Categories WHERE (CategoryID = @CategoryID)";
234 this.sqlUpdateCommand1.Connection = this.sqlConnection1;
235 this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CategoryName", System.Data.SqlDbType.NVarChar, 15, "CategoryName"));
236 this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Description", System.Data.SqlDbType.NVarChar, 1073741823, "Description"));
237 this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Picture", System.Data.SqlDbType.VarBinary, 2147483647, "Picture"));
238 this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CategoryID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CategoryID", System.Data.DataRowVersion.Original, null));
239 this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CategoryName", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CategoryName", System.Data.DataRowVersion.Original, null));
240 this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CategoryID", System.Data.SqlDbType.Int, 4, "CategoryID"));
241 //
242 // sqlDeleteCommand1
243 //
244 this.sqlDeleteCommand1.CommandText = "DELETE FROM Categories WHERE (CategoryID = @Original_CategoryID) AND (CategoryNam" +
245 "e = @Original_CategoryName)";
246 this.sqlDeleteCommand1.Connection = this.sqlConnection1;
247 this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CategoryID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CategoryID", System.Data.DataRowVersion.Original, null));
248 this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CategoryName", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CategoryName", System.Data.DataRowVersion.Original, null));
249 //
250 // sqlConnection1
251 //
252 this.sqlConnection1.ConnectionString = "data source=ALISA;initial catalog=Northwind;integrated security=SSPI;persist secu" +
253 "rity info=False;workstation id=ALISA;packet size=4096";
254 //
255 // sqlDataAdapter1
256 //
257 this.sqlDataAdapter1.DeleteCommand = this.sqlDeleteCommand1;
258 this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;
259 this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
260 this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
261 new System.Data.Common.DataTableMapping("Table", "Categories", new System.Data.Common.DataColumnMapping[] {
262 new System.Data.Common.DataColumnMapping("CategoryID", "CategoryID"),
263 new System.Data.Common.DataColumnMapping("CategoryName", "CategoryName"),
264 new System.Data.Common.DataColumnMapping("Description", "Description"),
265 new System.Data.Common.DataColumnMapping("Picture", "Picture")})});
266 this.sqlDataAdapter1.UpdateCommand = this.sqlUpdateCommand1;
267 //
268 // Form1
269 //
270 this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
271 this.ClientSize = new System.Drawing.Size(456, 285);
272 this.Controls.AddRange(new System.Windows.Forms.Control[] {
273 this.button2,
274 this.button1,
275 this.dataGrid1});
276 this.Name = "Form1";
277 this.Text = "Form1";
278 this.Load += new System.EventHandler(this.Form1_Load);
279 ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
280 ((System.ComponentModel.ISupportInitialize)(this.customersDataSet1)).EndInit();
281 this.ResumeLayout(false);
282
283 }
284 #endregion
285
286 /// <summary>
287 /// The main entry point for the application.
288 /// </summary>
289 [STAThread]
290 static void Main()
291 {
292 Application.Run(new Form1());
293 }
294
295 private void button1_Click(object sender, System.EventArgs e) {
296 CustomerDataSource cds = new CustomerDataSource();
297 cds.FillDataSet(customersDataSet1);
298 }
299
300 private void Form1_Load(object sender, System.EventArgs e) {
301 }
302
303 [System.Runtime.InteropServices.DllImport("User32.dll")]
304 private static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
305
306 private void button2_Click(object sender, System.EventArgs e) {
307 MessageBox.Show(customersDataSet1.GetChanges().GetXml());
308 }
309
310
311 }
312 }
313 |
| Вернуться к списку исходников в категории Winforms |
|
|
 |
 |
 |
 |
|
|