关于在不同控件事件中使用同一DATASET的问题
我想让dataGrid和listbox互动即dataset中name绑定到listbox中,listbox的选择发生变化时datagrid也相应发生选择变化;
代码如下:
public void cartype_Load(object sender, System.EventArgs e)
{
myconn = new SqlConnection ( this.cartype1.cnnstr ) ;
myconn.Open ( ) ;
string sqlstr = "select typeid as 系统代码,name as 车型名称,type as 车型代码 from cartype";
DataSet my = new DataSet("cartype");
SqlDataAdapter mydata = new SqlDataAdapter(sqlstr, myconn);
mydata.Fill(my,"cartype");
this.dataGrid1.DataSource=my.Tables["cartype"].DefaultView;
listBox1.DataSource=my;
listBox1.DisplayMember="cartype.车型名称";
listBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text",my,"cartype.车型名称"));
}
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
this.BindingContext[my,"cartype"].Position=listBox1.SelectedIndex;//这里中断提示:其他信息: 值不能为空。
}
我的问题时怎么在listBox1_SelectedIndexChanged用cartype_Load中的dataset