真是大开眼界,居然会有这样的错误

homesos 2004-09-09 01:47:36
************** 异常文本 **************
我在DataGrid上绑定DataSet对象(局部变量),居然有时会出现如下的错误:
错误一:
System.ArgumentException: 列“PID”不属于表 Table。
at System.Data.DataRow.CheckColumn(DataColumn column)
at System.Data.DataRow.get_Item(DataColumn column, DataRowVersion version)
at System.Data.DataRowView.GetColumnValue(DataColumn column)
at System.Data.DataColumnPropertyDescriptor.GetValue(Object component)
at System.Windows.Forms.DataGridColumnStyle.GetColumnValueAtRow(CurrencyManager source, Int32 rowNum)
at System.Windows.Forms.DataGridTextBoxColumn.Edit(CurrencyManager source, Int32 rowNum, Rectangle bounds, Boolean readOnly, String instantText, Boolean cellIsVisible)
at System.Windows.Forms.DataGrid.Edit(String instantText)
at System.Windows.Forms.DataGrid.Edit()
at System.Windows.Forms.DataGrid.OnEnter(EventArgs e)
at System.Windows.Forms.Control.NotifyEnter()
at System.Windows.Forms.ContainerControl.UpdateFocusedControl()

错误二:
************** 异常文本 **************
System.IndexOutOfRangeException: 索引 3 处没有值。
at System.Windows.Forms.CurrencyManager.get_Item(Int32 index)
at System.Windows.Forms.DataGridColumnStyle.GetColumnValueAtRow(CurrencyManager source, Int32 rowNum)
at System.Windows.Forms.DataGridTextBoxColumn.Edit(CurrencyManager source, Int32 rowNum, Rectangle bounds, Boolean readOnly, String instantText, Boolean cellIsVisible)
at System.Windows.Forms.DataGrid.Edit(String instantText)
at System.Windows.Forms.DataGrid.Edit()
at System.Windows.Forms.DataGrid.OnEnter(EventArgs e)
at System.Windows.Forms.Control.NotifyEnter()
at System.Windows.Forms.ContainerControl.UpdateFocusedControl()


但我在调试时,里面所说的错误根本不存在,也就是说DataSet对象中该有的都有,就是在绑定它至DataGrid中的时候,偶尔会出现这样的错误,把局部DataSet变量换成全局变量也如此,各位大侠,出现这样错误的原因可能是什么,是不是DataGrid在程序运行的过程中出现了异常!
求各位大虾帮帮忙吧,偶是百思不得其解呀
...全文
898 67 打赏 收藏 转发到动态 举报
写回复
用AI写文章
67 条回复
切换为时间正序
请发表友善的回复…
发表回复
homesos 2004-09-16
  • 打赏
  • 举报
回复
也谢谢大家的发言
homesos 2004-09-16
  • 打赏
  • 举报
回复
这个方法,真是太管用了,太谢谢你了
哈哈哈哈……
homesos 2004-09-14
  • 打赏
  • 举报
回复
谢了,我试试看
allanli 2004-09-13
  • 打赏
  • 举报
回复
这里把我解决这个异常的办法说一下,其实我也不是十分肯定是由于这个原因造成的,反正我这么做后就不会出现这个异常了.

我的DataGrid是有添加DataGridTableStyle,我在每次重新绑定数据源前都把DataGrid的DataGridTableStyle清除掉,绑定数据后再重新添加,那样就不会出现异常了.但这样带来的后果是拖慢了程序,因为清除样式再添加是非常耗时的,大概需要1秒的时间左右,看你的字段数而然.
W_W_Q 2004-09-13
  • 打赏
  • 举报
回复
我也碰到啊,原来是BUG。
allanli 2004-09-13
  • 打赏
  • 举报
回复
看了一下你的程序,的确没有装载DataGridTableStyle,但看了一下DataGrid的源代码,其实在调用SetDataBinding的时候,是有添加DataGridTableStyle,他使用了defaultTableStyle属性.你监视一下这个属性看看.还不行我也帮不了你了.
homesos 2004-09-13
  • 打赏
  • 举报
回复
allanli(若尘) 信件已发出,请查收!
谢谢辛苦了
91bct 2004-09-11
  • 打赏
  • 举报
回复
赞同allanli(若尘)所说的:
》原因是由于DataGrid装载了DataTableStyle造成的.不信的话你不要给DataGrid装载样式而直接绑定数据源。
homesos 2004-09-11
  • 打赏
  • 举报
回复
allanli(若尘) 信件已发出,请查收!
谢谢辛苦了
allanli 2004-09-11
  • 打赏
  • 举报
回复
allan_stu@163.com
homesos 2004-09-11
  • 打赏
  • 举报
回复
allanli(若尘) :
我的DataSet对象就是局部变量呀,每次绑定时不都是新的吗?
在绑定dataGrid1前,dataGrid1.TableStyles.count的值确是零
你有电邮是多少,我发份给你瞧瞧,麻烦了,多谢!
homesos 2004-09-11
  • 打赏
  • 举报
回复
SQL语句没有任何的错误,执行非常正常,每次都能得到正确的DataSet对象,里面的的结构与数据不少胳膊不少腿,只不过在绑定的时候,添加、修改的时候非常正确没有任何的异常,在单条删除时也很对,没错误,只不过是在选中多条批量删除时,第一条会有这样的错误,它会让你选择继续还是退出,如果选择继续,删除以后的几条时没有任何的错误,真是奇怪。
反正在绑定到DataGrid前的DataSet中的DataTable跟想象中的是完全一样的,但却还是出错,可这个错在绑定前的DataTable中是不存在的
allanli 2004-09-11
  • 打赏
  • 举报
回复
没有收到你的信啊!!!发到这吧 khal@Tom.com
jjq5200311 2004-09-10
  • 打赏
  • 举报
回复
关注一下 顶!
homesos 2004-09-10
  • 打赏
  • 举报
回复
当点击按钮删除表中单条记录后调用这个方法刷新表格数据时很正常
当选中多条,删除时,删除完第一条时会出这个错误,如果跳过在删除以后的几条时会很正常
或在选中要删除但在确认删除时取消,它也会刷新表格,会出错的
homesos 2004-09-10
  • 打赏
  • 举报
回复

string strSql = "SELECT Pid as 产品编号, Pname as 产品名称, Sdate as 生产日期, Edate as 有效日期, Companyname as 供销商名称, type as 型号, num as 数量, privoderprice as 供销商价格, markprice as 市场价, anuoprice as 阿诺价 FROM productinfo inner join Providerinfo on productinfo.Providerid = Providerinfo.Providerid;";


错误是这样的
************** 异常文本 **************
System.ArgumentException: 列“产品编号”不属于表 Table。
at System.Data.DataRow.CheckColumn(DataColumn column)
at System.Data.DataRow.get_Item(DataColumn column, DataRowVersion version)
at System.Data.DataRowView.GetColumnValue(DataColumn column)
at System.Data.DataColumnPropertyDescriptor.GetValue(Object component)
at System.Windows.Forms.DataGridColumnStyle.GetColumnValueAtRow(CurrencyManager source, Int32 rowNum)
at System.Windows.Forms.DataGridTextBoxColumn.Edit(CurrencyManager source, Int32 rowNum, Rectangle bounds, Boolean readOnly, String instantText, Boolean cellIsVisible)
at System.Windows.Forms.DataGrid.Edit(String instantText)
at System.Windows.Forms.DataGrid.Edit()
at System.Windows.Forms.DataGrid.OnEnter(EventArgs e)
at System.Windows.Forms.Control.NotifyEnter()
at System.Windows.Forms.ContainerControl.UpdateFocusedControl()
singleflower 2004-09-10
  • 打赏
  • 举报
回复
错了,是字段的ID标志
singleflower 2004-09-10
  • 打赏
  • 举报
回复
偶查了一下,是你的Pid字段有问题,默认下,他是代表数据库进程,所以最好将这个字段改名,或是写成这样:

SELECT productinfo.[Pid] as 产品编号,

试试
singleflower 2004-09-10
  • 打赏
  • 举报
回复
又长见识,能解决吗?
The_Gathering 2004-09-10
  • 打赏
  • 举报
回复
可能是删除了DataSet中的数据后没有更新DataGrid的CurrentRowIndex导致的
加载更多回复(47)

110,538

社区成员

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

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

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