DataSet的添加记录问题

yan1617 2003-11-07 10:40:26
下面代码是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;}
...全文
71 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
yan1617 2003-11-07
  • 打赏
  • 举报
回复
上面问题解决
怎么dv1.Sort = "cbh ASC ";没有排序啊
Happyboy_zjy 2003-11-07
  • 打赏
  • 举报
回复
考虑一下主键的问题。
for(int i=0;i<ListBox2.Items.Count;i++)
{
dr["cbh"]=ListBox2.Items[i];
cbh.Rows.Add(dr);
}
这样做重复循环?
RainInWind 2003-11-07
  • 打赏
  • 举报
回复
dr这个对象必须重新new一次!
//被选中的记录加入ListBox2
for(int i=0;i<ListBox2.Items.Count;i++)
{
dr =cbh.NewRow();
dr["cbh"]=ListBox2.Items[i];
cbh.Rows.Add(dr);
}
dr["cbh"]=aa;
cbh.Rows.Add(dr);

111,101

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧