string conn="server=.;user id=sa;pwd=;database=手术安排";
SqlConnection con=new SqlConnection(conn);
con.Open();
string sql="select * from 用户信息";
SqlCommand cmd=new SqlCommand(sql,con);
SqlDataAdapter sa=new SqlDataAdapter(sql,con);
DataSet ds=new DataSet();
sa.Fill(ds,"用户信息");
this.dataGrid1.DataSource=ds;
this.dataGrid1.DataMember="用户信息";
for(int i=0;i<ds.Tables["用户信息"].Rows.Count;i++)
{
this.textBox1.Text=ds.Tables["用户信息"].Rows[i]["编号"].ToString().Trim();
this.textBox2.Text=ds.Tables["用户信息"].Rows[i]["用户名"].ToString().Trim();
this.textBox3.Text=ds.Tables["用户信息"].Rows[i]["密码"].ToString().Trim();
}
上面的代码运行后,为什么在textbox中只能显示数据库表中的最后一行信息,我的目的是要绑定textbox控件,能显示表中每行的信息。
请问该如何修改,(如何用代码实现textbox的绑定)
SqlConnection con=new SqlConnection(conn);
con.Open();
string sql="select * from 用户信息";
SqlCommand cmd=new SqlCommand(sql,con);
SqlDataAdapter sa=new SqlDataAdapter(sql,con);
DataSet ds=new DataSet();
sa.Fill(ds,"用户信息");
this.dataGrid1.DataSource=ds;
this.dataGrid1.DataMember="用户信息";
for(int i=0;i<ds.Tables["用户信息"].Rows.Count;i++)
{
this.textBox1.Text=ds.Tables["用户信息"].Rows[i]["编号"].ToString().Trim();
this.textBox2.Text=ds.Tables["用户信息"].Rows[i]["用户名"].ToString().Trim();
this.textBox3.Text=ds.Tables["用户信息"].Rows[i]["密码"].ToString().Trim();
}
上面的代码运行后,为什么在textbox中只能显示数据库表中的最后一行信息,我的目的是要绑定textbox控件,能显示表中每行的信息。
请问该如何修改,(如何用代码实现textbox的绑定)
this.textBox2.Text +=ds.Tables["用户信息"].Rows[i]["用户名"].ToString().Trim();
this.textBox3.Text +=ds.Tables["用户信息"].Rows[i]["密码"].ToString().Trim();
{
textBox1.DataBindings.Add("Text", ds, "Customers.CustName");
textBox2.DataBindings.Add("Text", ds, "Customers.CustCode");
}private void Button1_Click(object sender, EventArgs e)
{
this.BindingContext[ds, "Customers"].Position += 1;
}