|
Связь DataGridView и Combobox
#36558234
Ссылка:
Ссылка на сообщение:
Ссылка с названием темы:
|
|
|
|
пример набросал
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130. 131. 132. 133. 134. 135. 136. 137. 138. 139. 140. 141. 142. 143. 144. 145. 146. 147. 148. 149. 150. 151. 152. 153. 154. 155. 156. 157. 158. 159. 160. 161. 162. 163. 164. 165. 166. 167. 168. 169. 170. 171. 172. 173. 174. 175. 176. 177. 178. 179. 180. 181. 182. 183. 184. 185. 186. 187. 188. 189. 190. 191. 192. 193. 194. 195. 196. 197. 198. 199. 200. 201. 202. 203. 204. 205. 206. 207. 208. 209. 210. 211. 212. 213. 214. 215. 216. 217. 218. 219. 220. 221.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace FilterGrid
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
public class Form1 : Form
{
private DataTable order = new DataTable("Order");
private DataTable salesPerson = new DataTable("SalesPerson");
private DataTable product = new DataTable("Product");
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.ComboBox comboBox2;
private System.Windows.Forms.ComboBox comboBox3;
private bool isLoading = false;
public Form1()
{
this.SuspendLayout();
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(465, 390);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
#region AddControls
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.comboBox2 = new System.Windows.Forms.ComboBox();
this.comboBox3 = new System.Windows.Forms.ComboBox();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(12, 12);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(441, 206);
this.dataGridView1.TabIndex = 0;
//
// comboBox1
//
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Location = new System.Drawing.Point(12, 224);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 21);
this.comboBox1.TabIndex = 1;
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
//
// comboBox2
//
this.comboBox2.FormattingEnabled = true;
this.comboBox2.Location = new System.Drawing.Point(153, 224);
this.comboBox2.Name = "comboBox2";
this.comboBox2.Size = new System.Drawing.Size(121, 21);
this.comboBox2.TabIndex = 2;
this.comboBox2.SelectedIndexChanged += new System.EventHandler(this.comboBox2_SelectedIndexChanged);
//Add Controls to Form
this.Controls.Add(this.comboBox2);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.dataGridView1);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
#endregion
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.Visible = true;
DataColumn col = new DataColumn("ID", typeof(int));
order.Columns.Add(col);
col = new DataColumn("SalesPersonID", typeof(int));
order.Columns.Add(col);
col = new DataColumn("ProductID", typeof(int));
order.Columns.Add(col);
col = new DataColumn("ID", typeof(int));
salesPerson.Columns.Add(col);
col = new DataColumn("FullName", typeof(string));
salesPerson.Columns.Add(col);
col = new DataColumn("ID", typeof(int));
product.Columns.Add(col);
col = new DataColumn("Name", typeof(string));
product.Columns.Add(col);
order.BeginInit();
DataRow row = order.NewRow();
row["ID"] = 1;
row["SalesPersonID"] = 1;
row["ProductID"] = 2;
order.Rows.Add(row);
row = order.NewRow();
row["ID"] = 2;
row["SalesPersonID"] = 2;
row["ProductID"] = 1;
order.Rows.Add(row);
row = order.NewRow();
row["ID"] = 3;
row["SalesPersonID"] = 1;
row["ProductID"] = 3;
order.Rows.Add(row);
row = order.NewRow();
row["ID"] = 4;
row["SalesPersonID"] = 1;
row["ProductID"] = 1;
order.Rows.Add(row);
row = order.NewRow();
row["ID"] = 5;
row["SalesPersonID"] = 1;
row["ProductID"] = 1;
order.Rows.Add(row);
order.AcceptChanges();
order.EndInit();
salesPerson.BeginInit();
row = salesPerson.NewRow();
row["ID"] = 1;
row["FullName"] = "John Connor";
salesPerson.Rows.Add(row);
row = salesPerson.NewRow();
row["ID"] = 2;
row["FullName"] = "Sarah Connor";
salesPerson.Rows.Add(row);
salesPerson.AcceptChanges();
salesPerson.EndInit();
product.BeginInit();
row = product.NewRow();
row["ID"] = 1;
row["Name"] = "Machine Gun";
product.Rows.Add(row);
row = product.NewRow();
row["ID"] = 2;
row["Name"] = "Granate";
product.Rows.Add(row);
row = product.NewRow();
row["ID"] = 3;
row["Name"] = "Desert Eagle";
product.Rows.Add(row);
product.AcceptChanges();
product.EndInit();
dataGridView1.DataSource = new DataView(order);
isLoading = true;
comboBox1.DataSource = new DataView(salesPerson);
comboBox1.DisplayMember = "FullName";
comboBox1.ValueMember = "ID";
comboBox2.DataSource = new DataView(product);
comboBox2.DisplayMember = "Name";
comboBox2.ValueMember = "ID";
isLoading = false;
ApplyFilter();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ApplyFilter();
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
ApplyFilter();
}
private void ApplyFilter()
{
if (isLoading)
return;
string filter = "SalesPersonID = " + comboBox1.SelectedValue.ToString()
+ "AND ProductID = " + comboBox2.SelectedValue.ToString();
((DataView)dataGridView1.DataSource).RowFilter = filter;
}
}
}
}
|
|
|