请选择 进入手机版 | 继续访问电脑版
查看: 599|回复: 0

[ASP.NET教程] 使用BindingManagerBase浏览绑定的数据

3万

主题

3万

帖子

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
100197
发表于 2015-11-23 18:11:17
<无详细内容>
  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.SqlClient;
  8. public class Form1 : System.Windows.Forms.Form {
  9. private System.Windows.Forms.TextBox textBox1;
  10. private System.Windows.Forms.TextBox textBox2;
  11. private System.Windows.Forms.Button buttonBack;
  12. private System.Windows.Forms.Button buttonNext;
  13. private System.Data.DataSet dataSet1;
  14. private System.ComponentModel.Container components = null;
  15. private BindingManagerBase bMgr;
  16. public Form1() {
  17. InitializeComponent();
  18. }
  19. private void InitializeComponent() {
  20. this.textBox1 = new System.Windows.Forms.TextBox();
  21. this.textBox2 = new System.Windows.Forms.TextBox();
  22. this.buttonBack = new System.Windows.Forms.Button();
  23. this.buttonNext = new System.Windows.Forms.Button();
  24. this.dataSet1 = new System.Data.DataSet();
  25. ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).BeginInit();
  26. this.SuspendLayout();
  27. this.textBox1.Location = new System.Drawing.Point(8, 8);
  28. this.textBox1.Name = "textBox1";
  29. this.textBox1.Size = new System.Drawing.Size(160, 20);
  30. this.textBox1.TabIndex = 0;
  31. this.textBox1.Text = "textBox1";
  32. this.textBox2.Location = new System.Drawing.Point(8, 40);
  33. this.textBox2.Name = "textBox2";
  34. this.textBox2.Size = new System.Drawing.Size(160, 20);
  35. this.textBox2.TabIndex = 1;
  36. this.textBox2.Text = "textBox2";
  37. this.buttonBack.Location = new System.Drawing.Point(24, 80);
  38. this.buttonBack.Name = "buttonBack";
  39. this.buttonBack.Size = new System.Drawing.Size(56, 23);
  40. this.buttonBack.TabIndex = 2;
  41. this.buttonBack.Text = "<< Back";
  42. this.buttonBack.Click += new System.EventHandler(this.buttonBack_Click);
  43. this.buttonNext.Location = new System.Drawing.Point(96, 80);
  44. this.buttonNext.Name = "buttonNext";
  45. this.buttonNext.Size = new System.Drawing.Size(56, 23);
  46. this.buttonNext.TabIndex = 3;
  47. this.buttonNext.Text = "Next>>";
  48. this.buttonNext.Click += new System.EventHandler(this.buttonNext_Click);
  49. this.dataSet1.DataSetName = "NewDataSet";
  50. this.dataSet1.Locale = new System.Globalization.CultureInfo("en-US");
  51. this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  52. this.ClientSize = new System.Drawing.Size(176, 108);
  53. this.Controls.Add(this.buttonNext);
  54. this.Controls.Add(this.buttonBack);
  55. this.Controls.Add(this.textBox2);
  56. this.Controls.Add(this.textBox1);
  57. this.Name = "Form1";
  58. this.Text = "Form1";
  59. this.Load += new System.EventHandler(this.Form1_Load);
  60. ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).EndInit();
  61. this.ResumeLayout(false);
  62. }
  63. static void Main() {
  64. Application.Run(new Form1());
  65. }
  66. private void Form1_Load(object sender, System.EventArgs e) {
  67. string connString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";
  68. string sql = @"select * from employee ";
  69. SqlConnection conn = new SqlConnection(connString);
  70. SqlDataAdapter da = new SqlDataAdapter(sql, conn);
  71. da.Fill(dataSet1, "employee");
  72. textBox1.DataBindings.Add("text", dataSet1, "employee.firstname");
  73. textBox2.DataBindings.Add("text", dataSet1, "employee.lastname");
  74. bMgr = this.BindingContext[dataSet1, "employee"];
  75. }
  76. private void buttonNext_Click(object sender, System.EventArgs e) {
  77. bMgr.Position += 1;
  78. }
  79. private void buttonBack_Click(object sender, System.EventArgs e) {
  80. bMgr.Position -= 1;
  81. }
  82. }
复制代码


回复

使用道具 举报