你可以直接使用TEXTBOX的DataBoundings来加入数据源的

解决方案 »

  1.   

    选中TextBox,在属性窗口的最上方有个"(DataBinding)",选中它,右边会出现一个按钮,点击这个按钮你就会明白了!!
      

  2.   

    下面是我全部源代码,现在textBox中可以显示一条记录了,但当我点击“下一条”按钮时,报错,没将对象引用到实例,我该如何解决?
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Data.OleDb;namespace WindowsApplication1
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.Button button3;
    private System.Windows.Forms.Button button4;
    private System.Windows.Forms.TextBox textBox2;

    private System.Data.DataSet dataSet1;
    private BindingManagerBase myBind;
    private System.Windows.Forms.TextBox textBox3;
    private System.ComponentModel.Container components=null;
    public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的

    //
    InitializeComponent();
    GetConnect();
    //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form Designer generated code
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.button1 = new System.Windows.Forms.Button();
    this.button2 = new System.Windows.Forms.Button();
    this.button3 = new System.Windows.Forms.Button();
    this.button4 = new System.Windows.Forms.Button();
    this.textBox2 = new System.Windows.Forms.TextBox();
    this.textBox3 = new System.Windows.Forms.TextBox();
    this.SuspendLayout();
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(80, 48);
    this.textBox1.Name = "textBox1";
    this.textBox1.TabIndex = 0;
    this.textBox1.Text = "textBox1";
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(16, 152);
    this.button1.Name = "button1";
    this.button1.TabIndex = 1;
    this.button1.Text = "button1";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // button2
    // 
    this.button2.Location = new System.Drawing.Point(96, 152);
    this.button2.Name = "button2";
    this.button2.TabIndex = 2;
    this.button2.Text = "button2";
    this.button2.Click += new System.EventHandler(this.button2_Click);
    // 
    // button3
    // 
    this.button3.Location = new System.Drawing.Point(176, 152);
    this.button3.Name = "button3";
    this.button3.TabIndex = 3;
    this.button3.Text = "button3";
    this.button3.Click += new System.EventHandler(this.button3_Click);
    // 
    // button4
    // 
    this.button4.Location = new System.Drawing.Point(256, 152);
    this.button4.Name = "button4";
    this.button4.TabIndex = 4;
    this.button4.Text = "button4";
    this.button4.Click += new System.EventHandler(this.button4_Click);
    // 
    // textBox2
    // 
    this.textBox2.Location = new System.Drawing.Point(80, 80);
    this.textBox2.Name = "textBox2";
    this.textBox2.TabIndex = 5;
    this.textBox2.Text = "textBox2";
    // 
    // textBox3
    // 
    this.textBox3.Location = new System.Drawing.Point(80, 112);
    this.textBox3.Name = "textBox3";
    this.textBox3.TabIndex = 6;
    this.textBox3.Text = "textBox3";
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(392, 293);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
      this.textBox3,
      this.textBox2,
      this.button4,
      this.button3,
      this.button2,
      this.button1,
      this.textBox1});
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load);
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    public void GetConnect()
    {
    try
    {
    string strconn="Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = access.mdb" ;
    OleDbConnection myole= new OleDbConnection(strconn);
    string strcom="select * from con";
    dataSet1=new DataSet();
    myole.Open(); OleDbDataAdapter oda=new OleDbDataAdapter(strcom,myole);
    oda.Fill(dataSet1,"con");
    textBox1.DataBindings.Add("Text",dataSet1,"con.id");
    textBox2.DataBindings.Add("Text",dataSet1,"con.content");
    textBox3.DataBindings.Add("Text",dataSet1,"con.memo");
    myole.Close();
    }
    catch(Exception exp)
    {
    MessageBox.Show(exp.ToString(),"错误");
    }
    }
    static void Main() 
    {
    Application.Run(new Form1());
    } private void Form1_Load(object sender, System.EventArgs e)
    {

    } private void timer1_Tick(object sender, System.EventArgs e)
    {

    } private void button1_Click(object sender, System.EventArgs e)
    {
    myBind.Position=0;
    } private void button2_Click(object sender, System.EventArgs e)
    {
    if(myBind.Position==0)
    MessageBox.Show("已到了第一条记录!");
    else
    myBind.Position-=1;
    } private void button3_Click(object sender, System.EventArgs e)
    {
    if(myBind.Position==myBind.Count-1)
    MessageBox.Show("已到了最后一条!");
    else 
    myBind.Position+=1;
    } private void button4_Click(object sender, System.EventArgs e)
    {
    myBind.Position=myBind.Count-1;
    }
    }
    }
      

  3.   

    在myole.Close();前面给myBind 赋值就可以了.
    myBind = this.BindingContext[dataSet1, "con"];
    完整代码如下:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Data.OleDb;namespace WindowsApplication1
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.Button button3;
    private System.Windows.Forms.Button button4;
    private System.Windows.Forms.TextBox textBox2; private System.Data.DataSet dataSet1;
    private BindingManagerBase myBind;
    private System.Windows.Forms.TextBox textBox3;
    private System.ComponentModel.Container components=null;
    public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的 //
    InitializeComponent();
    GetConnect();
    //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    }#region Windows Form Designer generated code
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.button1 = new System.Windows.Forms.Button();
    this.button2 = new System.Windows.Forms.Button();
    this.button3 = new System.Windows.Forms.Button();
    this.button4 = new System.Windows.Forms.Button();
    this.textBox2 = new System.Windows.Forms.TextBox();
    this.textBox3 = new System.Windows.Forms.TextBox();
    this.SuspendLayout();
    //
    // textBox1
    //
    this.textBox1.Location = new System.Drawing.Point(80, 48);
    this.textBox1.Name = "textBox1";
    this.textBox1.TabIndex = 0;
    this.textBox1.Text = "textBox1";
    //
    // button1
    //
    this.button1.Location = new System.Drawing.Point(16, 152);
    this.button1.Name = "button1";
    this.button1.TabIndex = 1;
    this.button1.Text = "button1";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    //
    // button2
    //
    this.button2.Location = new System.Drawing.Point(96, 152);
    this.button2.Name = "button2";
    this.button2.TabIndex = 2;
    this.button2.Text = "button2";
    this.button2.Click += new System.EventHandler(this.button2_Click);
    //
    // button3
    //
    this.button3.Location = new System.Drawing.Point(176, 152);
    this.button3.Name = "button3";
    this.button3.TabIndex = 3;
    this.button3.Text = "button3";
    this.button3.Click += new System.EventHandler(this.button3_Click);
    //
    // button4
    //
    this.button4.Location = new System.Drawing.Point(256, 152);
    this.button4.Name = "button4";
    this.button4.TabIndex = 4;
    this.button4.Text = "button4";
    this.button4.Click += new System.EventHandler(this.button4_Click);
    //
    // textBox2
    //
    this.textBox2.Location = new System.Drawing.Point(80, 80);
    this.textBox2.Name = "textBox2";
    this.textBox2.TabIndex = 5;
    this.textBox2.Text = "textBox2";
    //
    // textBox3
    //
    this.textBox3.Location = new System.Drawing.Point(80, 112);
    this.textBox3.Name = "textBox3";
    this.textBox3.TabIndex = 6;
    this.textBox3.Text = "textBox3";
    //
    // Form1
    //
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(392, 293);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
      this.textBox3,
      this.textBox2,
      this.button4,
      this.button3,
      this.button2,
      this.button1,
      this.textBox1});
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load);
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    public void GetConnect()
    {
    try
    {
    string strconn="Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = access.mdb" ;
    OleDbConnection myole= new OleDbConnection(strconn);
    string strcom="select * from con";
    dataSet1=new DataSet();
    myole.Open(); OleDbDataAdapter oda=new OleDbDataAdapter(strcom,myole);
    oda.Fill(dataSet1,"con");
    textBox1.DataBindings.Add("Text",dataSet1,"con.id");
    textBox2.DataBindings.Add("Text",dataSet1,"con.content");
    textBox3.DataBindings.Add("Text",dataSet1,"con.memo");
    myBind = this.BindingContext[dataSet1, "con"]; myole.Close();
    }
    catch(Exception exp)
    {
    MessageBox.Show(exp.ToString(),"错误");
    }
    }
    static void Main()
    {
    Application.Run(new Form1());
    } private void Form1_Load(object sender, System.EventArgs e)
    { } private void timer1_Tick(object sender, System.EventArgs e)
    { } private void button1_Click(object sender, System.EventArgs e)
    {
    myBind.Position=0;
    } private void button2_Click(object sender, System.EventArgs e)
    {
    if(myBind.Position==0)
    MessageBox.Show("已到了第一条记录!");
    else
    myBind.Position-=1;
    } private void button3_Click(object sender, System.EventArgs e)
    {
    if(myBind.Position==myBind.Count-1)
    MessageBox.Show("已到了最后一条!");
    else
    myBind.Position+=1;
    } private void button4_Click(object sender, System.EventArgs e)
    {
    myBind.Position=myBind.Count-1;
    }
    }
    }