[求助]为什么控件绑定数据后显示不正常?
想请教各位两个问题:1、象下面的文本框控件假入数量很多,想通过循环清理和重新绑定来达到减少代码录入量的目的应该怎样写代码?
2、为什么从下拉框中只有第一次选定的项数据显示正常,后面改选的项就有部分文本框没有数据据显示了?
代码如下。请各位帮忙解疑,谢谢!
private void combo1_SelectedIndexChanged(object sender, EventArgs e)
{
string Strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Application.StartupPath + "\\abc.mdb";
DataTable Tbl = new DataTable();
switch (combo1.SelectedItem.ToString())
{
case "aa":
this.textBox1.DataBindings.Clear();//移除数据绑定
this.textBox2.DataBindings.Clear();
this.textBox3.DataBindings.Clear();
this.textBox4.DataBindings.Clear();
this.textBox5.DataBindings.Clear();
this.textBox6.DataBindings.Clear();
Tbl.Clear();//数据表清理
string StrSql = "SELECT * FROM 变压器台帐";
OleDbDataAdapter da = new OleDbDataAdapter(StrSql,Strcon);//定义DataAdapter
da.Fill(Tbl);//填充表格
this.textBox1.DataBindings.Add("Text", Tbl, Tbl.Columns[0].Caption);//创建文本框字段绑定
this.textBox2.DataBindings.Add("Text", Tbl, Tbl.Columns[1].Caption);
this.textBox3.DataBindings.Add("Text", Tbl, Tbl.Columns[2].Caption);
this.textBox4.DataBindings.Add("Text", Tbl, Tbl.Columns[3].Caption);
this.textBox5.DataBindings.Add("Text", Tbl, Tbl.Columns[4].Caption);
this.textBox6.DataBindings.Add("Text", Tbl, Tbl.Columns[5].Caption);
break;
case "bb":
this.textBox1.DataBindings.Clear();
this.textBox2.DataBindings.Clear();
this.textBox3.DataBindings.Clear();
this.textBox4.DataBindings.Clear();
this.textBox5.DataBindings.Clear();
this.textBox6.DataBindings.Clear();
Tbl.Clear();
string StrSql1 = "SELECT * FROM 电动机台帐";
OleDbDataAdapter da1 = new OleDbDataAdapter(StrSql1,Strcon);
da1.Fill(Tbl);
this.textBox1.DataBindings.Add("Text", Tbl, Tbl.Columns[0].Caption);
this.textBox2.DataBindings.Add("Text", Tbl, Tbl.Columns[1].Caption);
this.textBox3.DataBindings.Add("Text", Tbl, Tbl.Columns[2].Caption);
this.textBox4.DataBindings.Add("Text", Tbl, Tbl.Columns[3].Caption);
this.textBox5.DataBindings.Add("Text", Tbl, Tbl.Columns[4].Caption);
this.textBox6.DataBindings.Add("Text", Tbl, Tbl.Columns[5].Caption);
break;
}
}
[此贴子已经被作者于2006-8-16 13:22:23编辑过]