Form1.cs
上传用户:yiyuerguo
上传日期:2014-09-27
资源大小:3781k
文件大小:7k
源码类别:

C#编程

开发平台:

Others

  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. using System.Data.OleDb;
  8. namespace DataGridApp
  9. {
  10. /// <summary>
  11. /// Form1 的摘要说明。
  12. /// </summary>
  13. public class Form1 : System.Windows.Forms.Form
  14. {
  15. private System.Data.DataSet dataSet1;
  16. private System.Windows.Forms.DataGrid dataGrid1;
  17. private System.Windows.Forms.Button button1;
  18. private System.Data.OleDb.OleDbCommand oleDbCommand1;
  19.         
  20. private OleDbCommandBuilder cb;
  21. private OleDbDataAdapter da;
  22. private System.Windows.Forms.Button button2;
  23. private System.Windows.Forms.Button button3;
  24. private System.Windows.Forms.TextBox textBox2;
  25. private System.Windows.Forms.TextBox textBox1;
  26.         BindingManagerBase bManager;
  27. private System.Windows.Forms.Button button4;
  28. /// <summary>
  29. /// 必需的设计器变量。
  30. /// </summary>
  31. private System.ComponentModel.Container components = null;
  32. public Form1()
  33. {
  34. //
  35. // Windows 窗体设计器支持所必需的
  36. //
  37. InitializeComponent();
  38. //
  39. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  40. //
  41. }
  42. /// <summary>
  43. /// 清理所有正在使用的资源。
  44. /// </summary>
  45. protected override void Dispose( bool disposing )
  46. {
  47. if( disposing )
  48. {
  49. if (components != null) 
  50. {
  51. components.Dispose();
  52. }
  53. }
  54. base.Dispose( disposing );
  55. }
  56. #region Windows Form Designer generated code
  57. /// <summary>
  58. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  59. /// 此方法的内容。
  60. /// </summary>
  61. private void InitializeComponent()
  62. {
  63. this.dataSet1 = new System.Data.DataSet();
  64. this.oleDbCommand1 = new System.Data.OleDb.OleDbCommand();
  65. this.button1 = new System.Windows.Forms.Button();
  66. this.dataGrid1 = new System.Windows.Forms.DataGrid();
  67. this.button2 = new System.Windows.Forms.Button();
  68. this.button3 = new System.Windows.Forms.Button();
  69. this.textBox2 = new System.Windows.Forms.TextBox();
  70. this.textBox1 = new System.Windows.Forms.TextBox();
  71. this.button4 = new System.Windows.Forms.Button();
  72. ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).BeginInit();
  73. ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
  74. this.SuspendLayout();
  75. // 
  76. // dataSet1
  77. // 
  78. this.dataSet1.DataSetName = "NewDataSet";
  79. this.dataSet1.Locale = new System.Globalization.CultureInfo("zh-CN");
  80. // 
  81. // button1
  82. // 
  83. this.button1.Location = new System.Drawing.Point(264, 224);
  84. this.button1.Name = "button1";
  85. this.button1.TabIndex = 0;
  86. this.button1.Text = "数据刷新";
  87. this.button1.Click += new System.EventHandler(this.button1_Click);
  88. // 
  89. // dataGrid1
  90. // 
  91. this.dataGrid1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
  92. | System.Windows.Forms.AnchorStyles.Right)));
  93. this.dataGrid1.DataMember = "";
  94. this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
  95. this.dataGrid1.Location = new System.Drawing.Point(8, 72);
  96. this.dataGrid1.Name = "dataGrid1";
  97. this.dataGrid1.Size = new System.Drawing.Size(336, 136);
  98. this.dataGrid1.TabIndex = 1;
  99. this.dataGrid1.Navigate += new System.Windows.Forms.NavigateEventHandler(this.dataGrid1_Navigate);
  100. // 
  101. // button2
  102. // 
  103. this.button2.Location = new System.Drawing.Point(8, 224);
  104. this.button2.Name = "button2";
  105. this.button2.TabIndex = 7;
  106. this.button2.Text = "Next>>";
  107. this.button2.Click += new System.EventHandler(this.button2_Click);
  108. // 
  109. // button3
  110. // 
  111. this.button3.Location = new System.Drawing.Point(96, 224);
  112. this.button3.Name = "button3";
  113. this.button3.TabIndex = 6;
  114. this.button3.Text = "<<Back";
  115. this.button3.Click += new System.EventHandler(this.button3_Click);
  116. // 
  117. // textBox2
  118. // 
  119. this.textBox2.Location = new System.Drawing.Point(8, 40);
  120. this.textBox2.Name = "textBox2";
  121. this.textBox2.Size = new System.Drawing.Size(216, 21);
  122. this.textBox2.TabIndex = 5;
  123. this.textBox2.Text = "";
  124. this.textBox2.TextChanged += new System.EventHandler(this.textBox2_TextChanged);
  125. // 
  126. // textBox1
  127. // 
  128. this.textBox1.Location = new System.Drawing.Point(8, 8);
  129. this.textBox1.Name = "textBox1";
  130. this.textBox1.Size = new System.Drawing.Size(216, 21);
  131. this.textBox1.TabIndex = 4;
  132. this.textBox1.Text = "";
  133. // 
  134. // button4
  135. // 
  136. this.button4.Location = new System.Drawing.Point(184, 224);
  137. this.button4.Name = "button4";
  138. this.button4.TabIndex = 8;
  139. this.button4.Text = "删除";
  140. this.button4.Click += new System.EventHandler(this.button4_Click);
  141. // 
  142. // Form1
  143. // 
  144. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  145. this.ClientSize = new System.Drawing.Size(352, 253);
  146. this.Controls.Add(this.button4);
  147. this.Controls.Add(this.button2);
  148. this.Controls.Add(this.button3);
  149. this.Controls.Add(this.textBox2);
  150. this.Controls.Add(this.textBox1);
  151. this.Controls.Add(this.dataGrid1);
  152. this.Controls.Add(this.button1);
  153. this.Name = "Form1";
  154. this.Text = "Form1";
  155. this.Load += new System.EventHandler(this.Form1_Load);
  156. ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).EndInit();
  157. ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
  158. this.ResumeLayout(false);
  159. }
  160. #endregion
  161. /// <summary>
  162. /// 应用程序的主入口点。
  163. /// </summary>
  164. [STAThread]
  165. static void Main() 
  166. {
  167. Application.Run(new Form1());
  168. }
  169. private void Form1_Load(object sender, System.EventArgs e)
  170. {
  171. string ConStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=SimpleBank.mdb";
  172.     
  173. // 连接数据源 相当于水龙头接上水管 
  174. OleDbConnection Conn = new OleDbConnection(ConStr);
  175.       
  176. // 建立数据库命令列表
  177. oleDbCommand1 = new OleDbCommand("SELECT * FROM Account", Conn);
  178. // 实例化 oleDbCommand1对象
  179. da = new OleDbDataAdapter();
  180. da.SelectCommand = oleDbCommand1;
  181. //实例化 OleDbCommandBuilder 对象 否则无法进行数据刷新
  182. cb = new OleDbCommandBuilder(da);
  183. // 找到数据集dataSet1 就是找到了一个可以接水的容器
  184. da.Fill(dataSet1, "Account");
  185. // 在运行状态中绑定数据库与数据表
  186. // 这属于一种复杂绑定
  187. dataGrid1.SetDataBinding(dataSet1, "Account");
  188. // 绑定TextBox1的内容与Account.AccountID
  189.             // 这属于一种简单绑定
  190. textBox1.DataBindings.Add("Text", dataSet1, "Account.AccountID");
  191. // 绑定TextBox1的内容与Account.Owner
  192. // 这属于一种简单绑定
  193. textBox2.DataBindings.Add("Text", dataSet1, "Account.Owner");
  194. // 建立一个dataSet1的管理器 来控制当前数据的位置
  195. bManager = this.BindingContext [dataSet1, "Account"];
  196. }
  197. private void button1_Click(object sender, System.EventArgs e)
  198. {
  199. da.Update(dataSet1, "Account");
  200. }
  201. private void dataGrid1_Navigate(object sender, System.Windows.Forms.NavigateEventArgs e)
  202. {
  203. }
  204. private void button3_Click(object sender, System.EventArgs e)
  205. {
  206.     bManager.Position -=1;
  207. }
  208. private void button2_Click(object sender, System.EventArgs e)
  209. {
  210. bManager.Position +=1;
  211. }
  212. private void button4_Click(object sender, System.EventArgs e)
  213. {
  214.    dataSet1.Tables["Account"].Rows[bManager.Position].Delete();
  215. }
  216. private void textBox2_TextChanged(object sender, System.EventArgs e)
  217. {
  218. }
  219. }
  220. }