DataSet的添加记录问题
下面代码是ListBox1中选中的数据传到ListBox2中。第一次传正常,第二次(其实只要向有数据的框中传值都这样)传出异常信息为“该行已经属于此表”。
try
{
string aa=ListBox1.SelectedItem.Value;
//删除ListBox1的被选中的记录
ListBox1.Items.Remove(aa);
CBH cbhact=new CBH();
sql="select distinct cbh from dbo.cbh";
ds=new DataSet();
ds=cbhact.GetInfo(sql);
ds.Tables[0].Clear();
DataTable cbh = ds.Tables[0];
cbh.Clear();
DataRow dr =cbh.NewRow();
//被选中的记录加入ListBox2
for(int i=0;i<ListBox2.Items.Count;i++)
{
dr["cbh"]=ListBox2.Items[i];
cbh.Rows.Add(dr);
}
dr["cbh"]=aa;
cbh.Rows.Add(dr); //////出现异常行/////
//填充ListBox2
dv1=new DataView();
dv1=ds.Tables[0].Copy().DefaultView;
dv1.Sort = "cbh ASC ";
foreach(DataRow dr1 in dv1.Table.Rows)
{
// Read every tables from rows
ListBox2.Items.Add(dr1["cbh"].ToString());
}
}
catch(Exception ex){Label7.Text=ex.Message;}