刚学.net,做个省市县联动的combobox组
我的做法是用BindingSource 绑定DataSet,再传给combobox
这样,我就要做三个DataSet和BindingSource。功能是可以实现的
但我想应该有更节约资源的做法吧,能否有“一次性”存储数据的东东,把数据给combobox之后,就跟combobox解除关联,而不是像dataset一样会动态更新。附代码(省级的combobox改变的方法):
private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
{
objCmd.CommandText = "select * from table where fid=" + comboBox1.SelectedValue;
objAdapter.SelectCommand = objCmd;
dataSet2 = new DataSet();
objAdapter.Fill(dataSet2, "gbsubject"); BindingSource bs = new BindingSource();
bs.DataMember = "table";
bs.DataSource = dataSet2;
comboBox2.DisplayMember = "sname";
comboBox2.ValueMember = "sid";
comboBox2.DataSource = bs;
comboBox3.DataSource = null;
}
我的做法是用BindingSource 绑定DataSet,再传给combobox
这样,我就要做三个DataSet和BindingSource。功能是可以实现的
但我想应该有更节约资源的做法吧,能否有“一次性”存储数据的东东,把数据给combobox之后,就跟combobox解除关联,而不是像dataset一样会动态更新。附代码(省级的combobox改变的方法):
private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
{
objCmd.CommandText = "select * from table where fid=" + comboBox1.SelectedValue;
objAdapter.SelectCommand = objCmd;
dataSet2 = new DataSet();
objAdapter.Fill(dataSet2, "gbsubject"); BindingSource bs = new BindingSource();
bs.DataMember = "table";
bs.DataSource = dataSet2;
comboBox2.DisplayMember = "sname";
comboBox2.ValueMember = "sid";
comboBox2.DataSource = bs;
comboBox3.DataSource = null;
}
然后再dataSet.Tables[0].Select("条件")
那么dataSet.Tables[0].Select("条件")的数据赋给谁,给BindingSource 还是直接作为combobox的datasource?
comboBox1.DisplayMember = "sname";
comboBox1.ValueMember = "sid";
comboBox1.DataSource = dataSet.Tables["table"].Select("fid=0");
结果combobox里的都是DataRow
DataTable dt=new DataTable();
foreach(DataRow dr in drs)
{
dt.Rows.Add(dr);
}
comboBox1.DataSource =dt;
Try....
这行提示“该行已属于另一个表”
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
DataView dv = new DataView(ds.Tables[1]);
dv.RowFilter = "UserID=" + comboBox1.SelectedValue; listBox1.DisplayMember = "UserInterest";
listBox1.ValueMember = "id";
listBox1.DataSource = dv;
}
catch
{
} }
不好意思,之前没有测试。。
这个错误可以改成dt.Rows.Add(dr.ItemArray);来避免。。
然后在DataTable dt=new DataTable();还要加dt.Columns.Add("列名");有几列加几列。。不过这个方法比不上8楼用DataView。。