我记得以前delphi有个这个这样的导航控件.点一下邦定的texbox控件自动读取下一条记录

解决方案 »

  1.   

    http://www.codeproject.com/KB/aspnet/NavigateControlRecords.aspx
      

  2.   

    搞出来了
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    namespace WindowsApplication3
    {
        public partial class Form1 : Form
        {
            int i = 0;//i用来去顶其所在数据表的行号,一开始为第一行
            DataSet ds;
            SqlDataAdapter da;
            string str = "Data Source=20111229-1228\\SQLEXPRESS;Initial Catalog=test;User ID=sa;Password=longhai";
            string strsql = "select * from student";
            SqlConnection cn;        public Form1()
            {
                InitializeComponent();
            }        private void button8_Click(object sender, EventArgs e)
            {
                int k = 0;//用来确定要查找数据行的下标
                if (textBox5.Text.Trim() == string.Empty)
                {
                    MessageBox.Show("查找内容不能为空!");
                    textBox5.Focus();
                }
                else
                    for (k = 0; k < ds.Tables["student"].Rows.Count; k++)
                        if (ds.Tables["student"].Rows[k]["id"].ToString() == textBox5.Text) //找到textBox5.Text文本框中相匹配的数据行,其行号为k
                        {
                            i = k;
                            show(i);
                            break;
                        }
                if (k == ds.Tables["student"].Rows.Count)
                    MessageBox.Show("无该学生信息");
                textBox5.Text = "";        }
            void show(int i)
            {
                try
                {
                    if (ds != null)
                        ds.Clear();
                    else
                    {
                        ds = new DataSet();
                        da = new SqlDataAdapter(strsql, str);
                    }
                    //由于DataSet为断开连接的,因此在显示时对数据集重新填入,从而显示的为更新后的值
                    da.Fill(ds, "student");
                    DataRow dr = ds.Tables["student"].Rows[i];
                    textBox1.Text = dr["id"].ToString();
                    textBox2.Text = dr["name"].ToString();
                    textBox3.Text = dr["department"].ToString();
                    textBox4.Text = dr["age"].ToString();
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }        private void Form1_Load(object sender, EventArgs e)
            {
                cn = new SqlConnection(str);
                show(i);     
            }        private void button4_Click(object sender, EventArgs e)
            {
                i = 0;
                show(i);
            }        private void button1_Click(object sender, EventArgs e)
            {
                SqlConnection cn = new SqlConnection(str);
                cn.Open();
                SqlCommand Sqlcmd = new SqlCommand("select count(*) from student where id='" + textBox1.Text.Trim() + "'", cn);            // Sqlcmd.Connection.Open();
                int a = (int)Sqlcmd.ExecuteScalar();//返回的为源表中与输入的学号相等的个数
                Sqlcmd.Connection.Close();
                if (a > 0)
                {
                    MessageBox.Show("此学号已存在,请重新输入");
                    return;
                }
                //上述方法通过求出在student表中,输入的学号与表中的原有数据是否冲突
                ds.Clear();
                da.Fill(ds, "student");
                ds.Tables["student"].PrimaryKey = new DataColumn[] { ds.Tables["student"].Columns["id"] };//设置数据表student 表的主码,为后来的使用Update 方法打下基础
                SqlCommandBuilder sqlCommandBuilder1 = new SqlCommandBuilder(da); //通过 CommandBulider对象自动生成DbDataAdapter 的**Command属性
                DataRow dr = ds.Tables["student"].NewRow();
                dr["id"] = textBox1.Text;
                dr["name"] = textBox2.Text;
                dr["department"] = textBox3.Text;
                if (textBox4.Text != "")
                    dr["age"] = int.Parse(textBox4.Text);
                else
                    dr["age"] = 0;
                ds.Tables["student"].Rows.Add(dr);
                da.Update(ds, "student");
                ds.AcceptChanges();
                MessageBox.Show("数据插入成功");
            }        private void button2_Click(object sender, EventArgs e)
            {
                for (int k = 0; k < ds.Tables["student"].Rows.Count; k++)
                    if (ds.Tables["student"].Rows[k]["id"].ToString() == "" + textBox1.Text + "" && ds.Tables["student"].Rows[k]["id"].ToString() != ds.Tables["student"].Rows[i]["id"].ToString())
                    {
                        MessageBox.Show("此学号已存在,请重新输入");
                        textBox1.Text = "";
                        return;
                    }
                ds.Clear();
                da.Fill(ds, "student");
                ds.Tables["student"].PrimaryKey = new DataColumn[] { ds.Tables["student"].Columns["id"] };
                SqlCommandBuilder sqlCommandBuilder1 = new SqlCommandBuilder(da);
                ds.BeginInit();//挂起修改;
                ds.Tables["student"].Rows[i]["id"] = textBox1.Text;
                ds.Tables["student"].Rows[i]["name"] = textBox2.Text;
                ds.Tables["student"].Rows[i]["department"] = textBox3.Text;
                if (textBox4.Text != "")
                    ds.Tables["student"].Rows[i]["age"] = int.Parse(textBox4.Text);
                else
                    ds.Tables["student"].Rows[i]["age"] = 0;
                ds.EndInit();//终止挂起修改
                da.Update(ds, "student");
                ds.AcceptChanges();
                MessageBox.Show("数据修改成功");
                Refresh();
            }        private void button3_Click(object sender, EventArgs e)
            {
                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(strsql, str);
                da.Fill(ds, "student");
                ds.Tables["student"].PrimaryKey = new DataColumn[] { ds.Tables["student"].Columns["id"] };
                SqlCommandBuilder sqlCommandBuilder1 = new SqlCommandBuilder(da);
                ds.Tables["student"].Rows[i].Delete();
                da.Update(ds, "student");
                ds.AcceptChanges();
                MessageBox.Show("数据删除成功");
                i = 0;
                show(i);        }        private void button5_Click(object sender, EventArgs e)
            {
                if (i != 0)
                {
                    i--;
                    show(i);
                }
                else
                    MessageBox.Show("已经是第一条记录");        }        private void button6_Click(object sender, EventArgs e)
            {
                if (i < ds.Tables["student"].Rows.Count - 1)
                {
                    i++;
                    show(i);
                }
                else
                    MessageBox.Show("已经是最后一条记录");
            }        private void button7_Click(object sender, EventArgs e)
            {
                i = ds.Tables["student"].Rows.Count - 1;
                show(i);
            }
        }
    }
      

  3.   

    你是学生吧??说明白了就是一个查找上一条与下一条你可以用AJAX。编程风格再细化一下不过你能做出来你的前程很光明白真的不错!加油