一个listbox 显示NorthWind数据库Costomer表的CostomerId字段
同过listbox的SelectedIndexChanged事件,当点击不同CostomerId
时,textbox将显示和CostomerId相应的其它字段,但在textbox与ds的
绑定是老是错,希望给点意见或代码示例
有点进展了
private void Form1_Load(object sender, EventArgs e)
{
string str = "select distinct CustomerID from Customers ";
da = new SqlDataAdapter(str, "server=LocalHost;integrated security=sspi;database=NorthWind");
ds = new DataSet();
da.Fill(ds,"Constomers");
listBox1.DisplayMember = "CustomerID";
listBox1.DataSource = ds.Tables["Constomers"];
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int index = int.Parse(this.listBox1.SelectedIndex.ToString());
textBox1.Text = ds.Tables["Constomers"].Rows[index][0].ToString() ;
// textBox1.Text = ds.Tables["Constomers"].Rows[index]["CustomerId"].ToString() ;
}
为什么只有当Rows[index][0]或Rows[index]["CustomerId"]是才会我想要的效果,而将Rows[index][0]
改为Rows[index][1],系统包错说"找不到Constomers的第一列"(改为Rows[index]["CompanyName"],也一样报错)