using System.Data.SqlClient;
namespace 學生信息浏覽
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
BindingSource bs = new BindingSource();
private void Form1_Load(object sender, EventArgs e)
{
DataSet ds = new DataSet("student2");
DataTable dt = new DataTable("T_Student");
dt.Columns.Add("StuID", typeof(string));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Sex", typeof(string));
dt.Columns.Add("Age", typeof(short));
dt.Columns.Add("Class", typeof(string));
dt.PrimaryKey = new DataColumn[] { dt.Columns[0] };
dt.Rows.Add("09001","王小玲","女",18,"軟件091");
dt.Rows.Add("09002", "張偉", "男", 20, "軟件092");
dt.Rows.Add("09001", "謝明明", "男", 19, "軟件091");
ds.Tables.Add(dt);
bs.DataSource = ds.Tables[0];
txtNo.DataBindings.Add("Text", bs, "StuID");
txtName.DataBindings.Add("Text", bs, "Name");
txtSex.DataBindings.Add("Text", bs, "Sex");
txtAge.DataBindings.Add("Text", bs, "Age");
txtClass.DataBindings.Add("Text", bs, "Class");
}
private void btnPrevious_Click(object sender, EventArgs e)
{
bs.MovePrevious();
}
private void btnNext_Click(object sender, EventArgs e)
{
bs.MoveNext();
}
}
}
如果你可以StuID重復的話,把dt.PrimaryKey = new DataColumn[] { dt.Columns[0] };這句刪除,否則就把
dt.Rows.Add("09001", "謝明明", "男", 19, "軟件091");
修改成
dt.Rows.Add("09003", "謝明明", "男", 19, "軟件091");