C# 给 dataGridView 动态添加行,下面的代码,一直报错

Jlins 2015-08-27 01:39:00
C# 给 dataGridView 动态添加行,下面的代码,一直报错

DataGridViewRow dgvr = new DataGridViewRow();
foreach (DataGridViewColumn c in this.dataGridView2.Columns)
{
dgvr.Cells.Add(c.CellTemplate.Clone() as DataGridViewCell);
}
dgvr.Cells[0].Value = 1;
dgvr.Cells[1].Value = "张三";
dgvr.Cells[2].Value = "深圳";
dgvr.Cells[3].Value = "张三";
dgvr.Cells[4].Value = "深圳";
this.dataGridView2.Rows.Add(dgvr);




“System.InvalidOperationException”类型的第一次机会异常在 System.Windows.Forms.dll 中发生
在 System.Windows.Forms.Control.get_Handle()
在 System.Windows.Forms.Control.SetVisibleCore(Boolean value)
在 System.Windows.Forms.Control.set_Visible(Boolean value)
在 System.Windows.Forms.DataGridView.LayoutScrollBars()
在 System.Windows.Forms.DataGridView.ComputeLayout()
在 System.Windows.Forms.DataGridView.PerformLayoutPrivate(Boolean useRowShortcut, Boolean computeVisibleRows, Boolean invalidInAdjustFillingColumns, Boolean repositionEditingControl)
在 System.Windows.Forms.DataGridView.ResetUIState(Boolean useRowShortcut, Boolean computeVisibleRows)
在 System.Windows.Forms.DataGridViewRowCollection.OnCollectionChanged_PreNotification(CollectionChangeAction cca, Int32 rowIndex, Int32 rowCount, DataGridViewRow& dataGridViewRow, Boolean changeIsInsertion)
在 System.Windows.Forms.DataGridViewRowCollection.OnCollectionChanged(CollectionChangeEventArgs e, Int32 rowIndex, Int32 rowCount, Boolean changeIsDeletion, Boolean changeIsInsertion, Boolean recreateNewRow, Point newCur线程 '<无名称>' (0x219c) 已退出,返回值为 0 (0x0)。
rentCell)
在 System.Windows.Forms.DataGridViewRowCollection.InsertInternal(Int32 rowIndex, DataGridViewRow dataGridViewRow, Boolean force)
在 System.Windows.Forms.DataGridViewRowCollection.InsertInternal(Int32 rowIndex, DataGridViewRow dataGridViewRow)
在 System.Windows.Forms.DataGridViewRowCollection.AddInternal(DataGridViewRow dataGridViewRow)
在 System.Windows.Forms.Da
taGridViewRowCollection.Add(DataGridViewRow dataGridViewRow)

...全文
805 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2015-08-27
  • 打赏
  • 举报
回复
引用 20 楼 dyllove98 的回复:
原来C#多线程中,子线程不能操作ui线程呀....怎么搞的和android似的。。。 搞定了 谢谢大家。
委托。
-小蕾- 2015-08-27
  • 打赏
  • 举报
回复
晕,怎么还没写完就发出去了。。。 算了,搞定了就行了。
-小蕾- 2015-08-27
  • 打赏
  • 举报
回复
DataView dv=dataGridView2.DataSource as DataView; if(dv==null) { DataTable datatable=new DataTable(); datatable.Columns.Add("id"); datatable.Columns.Add("id"); datatable.Columns.Add("id"); }
Jlins 2015-08-27
  • 打赏
  • 举报
回复
原来C#多线程中,子线程不能操作ui线程呀....怎么搞的和android似的。。。 搞定了 谢谢大家。
Jlins 2015-08-27
  • 打赏
  • 举报
回复
引用 18 楼 ajianchina 的回复:
把你现用的dataGridView2快快删掉,重新拖一个上来,重命名为dataGridView2 你这个可能还绑了一些事件在上面
兄弟 我发现和线程有关系,我不是用线程更新就可以,放到线程里面更新就不行。这个能解决吗?
ajianchina 2015-08-27
  • 打赏
  • 举报
回复
把你现用的dataGridView2快快删掉,重新拖一个上来,重命名为dataGridView2 你这个可能还绑了一些事件在上面
Jlins 2015-08-27
  • 打赏
  • 举报
回复
搞不定。。现在的情况是,点击“立刻发标” 按钮,后台会报错。 dataGridView 没有任何变化,如果这个时候手动点击 dataGridView 的标题,那么数据就会出现。 好像是 dataGridView 在展示改行的时候有问题。。。下面是添加时候的报错日志 “System.InvalidOperationException”类型的第一次机会异常在 System.Windows.Forms.dll 中发生 在 System.Windows.Forms.Control.get_Handle() 在 System.Windows.Forms.Control.SetVisibleCore(Boolean value) 在 System.Windows.Forms.Control.set_Visible(Boolean value) 在 System.Windows.Forms.DataGridView.LayoutScrollBars() 在 System.Windows.Forms.DataGridView.ComputeLayout() 在 System.Windows.Forms.DataGridView.PerformLayoutPrivate(Boolean useRowShortcut, Boolean computeVisibleRows, Boolean invalidInAdjustFillingColumns, Boolean repositionEditingControl) 在 System.Windows.Forms.DataGridView.ResetUIState(Boolean useRowShortcut, Boolean computeVisibleRows) 在 System.Windows.Forms.DataGridViewRowCollection.OnCollectionChanged_PreNotification(CollectionChangeAction cca, Int32 rowIndex, Int32 rowCount, DataGridViewRow& dataGridViewRow, Boolean changeIsInsertion) 在 System.Windows.Forms.DataGridViewRowCollection.OnCollectionChanged(CollectionChangeEventArgs e, Int32 rowIndex, Int32 rowCount, Boolean changeIsDeletion, Boolean changeIsInsertion, Boolean recreateNewRow, Point newCur线程 '<无名称>' (0x219c) 已退出,返回值为 0 (0x0)。 rentCell) 在 System.Windows.Forms.DataGridViewRowCollection.InsertInternal(Int32 rowIndex, DataGridViewRow dataGridViewRow, Boolean force) 在 System.Windows.Forms.DataGridViewRowCollection.InsertInternal(Int32 rowIndex, DataGridViewRow dataGridViewRow) 在 System.Windows.Forms.DataGridViewRowCollection.AddInternal(DataGridViewRow dataGridViewRow) 在 System.Windows.Forms.DataGridViewRowCollection.Add(DataGridViewRow dataGridViewRow)
Jlins 2015-08-27
  • 打赏
  • 举报
回复
引用 14 楼 ajianchina 的回复:
[quote=引用 13 楼 ajianchina的回复:]呵呵,原来你没绑定啊,那自然列要出错的,设一下列总数肯定就好了 dataGridView2.ColumnCount = 5;
还有,既然是动态的,你就别手动设置列了,将手动设置的全去掉,在Form_Load里加上这样一组代码: dataGridView2.ColumnCount = 5; dataGridView2.Columns[0].Name = "标题"; dataGridView2.Columns[1].Name = "金额"; dataGridView2.Columns[2].Name = "利率"; dataGridView2.Columns[3].Name = "进度"; dataGridView2.Columns[4].Name = "状态"; 然后你动态增加行数据,肯定没问题。[/quote] 我先试验下
Jlins 2015-08-27
  • 打赏
  • 举报
回复
这个是初始化的代码
this.dataGridView2.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView2.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.dataGridViewTextBoxColumn1,
this.dataGridViewTextBoxColumn2,
this.dataGridViewTextBoxColumn3,
this.dataGridViewTextBoxColumn5,
this.dataGridViewTextBoxColumn6});
this.dataGridView2.Location = new System.Drawing.Point(8, 54);
this.dataGridView2.Name = "dataGridView2";
this.dataGridView2.RowTemplate.Height = 23;
this.dataGridView2.Size = new System.Drawing.Size(516, 424);
this.dataGridView2.StandardTab = true;
this.dataGridView2.TabIndex = 24;
this.dataGridView2.ColumnCount = 5;


下面的是在线程中动态添加的代码

  DataGridViewRow dgvr = new DataGridViewRow();
foreach (DataGridViewColumn c in this.dataGridView2.Columns)
{
dgvr.Cells.Add(c.CellTemplate.Clone() as DataGridViewCell);
}
dgvr.Cells[0].Value = 1;
dgvr.Cells[1].Value = "张三";
dgvr.Cells[2].Value = "深圳";
dgvr.Cells[3].Value = "张三";
dgvr.Cells[4].Value = "深圳";
this.dataGridView2.Rows.Add(dgvr);
this.dataGridView2.Refresh();


ajianchina 2015-08-27
  • 打赏
  • 举报
回复
引用 13 楼 ajianchina的回复:
呵呵,原来你没绑定啊,那自然列要出错的,设一下列总数肯定就好了 dataGridView2.ColumnCount = 5;
还有,既然是动态的,你就别手动设置列了,将手动设置的全去掉,在Form_Load里加上这样一组代码: dataGridView2.ColumnCount = 5; dataGridView2.Columns[0].Name = "标题"; dataGridView2.Columns[1].Name = "金额"; dataGridView2.Columns[2].Name = "利率"; dataGridView2.Columns[3].Name = "进度"; dataGridView2.Columns[4].Name = "状态"; 然后你动态增加行数据,肯定没问题。
ajianchina 2015-08-27
  • 打赏
  • 举报
回复
呵呵,原来你没绑定啊,那自然列要出错的,设一下列总数肯定就好了 dataGridView2.ColumnCount = 5;
老李家的小二 2015-08-27
  • 打赏
  • 举报
回复
哥们,这是小问题,跟踪一下即可
於黾 2015-08-27
  • 打赏
  • 举报
回复
foreach (DataGridViewColumn c in this.dataGridView2.Columns) 首先你得确保已经给DGV添加了列
Jlins 2015-08-27
  • 打赏
  • 举报
回复
引用 4 楼 guwei4037 的回复:
你这段代码我刚试过了,没有报错。 其实更直观的写法是操作DataTable,给DataTable增加行。然后直接绑定datagridview1即可。
我是搞java的,偶尔用c# 做个小项目....所以很多不懂
Jlins 2015-08-27
  • 打赏
  • 举报
回复
引用 7 楼 ajianchina 的回复:
被绑定了数据源的DataGridView不能这样动态新增行的,数据源中新增后重新绑定。
我就拖拽了一个 ,没有绑定任何数据源。。。
Jlins 2015-08-27
  • 打赏
  • 举报
回复
引用 5 楼 starfd 的回复:
你的数据是怎么绑定上去的?
我没绑定数据源,我就是拖拽一个 dataGridView ,设置了几列,然后动态向里面添加数据
Jlins 2015-08-27
  • 打赏
  • 举报
回复
我发现,我执行后,必须在table上点一下才能出现? 是不是要刷新 dataGridView ? 如果刷新?
  • 打赏
  • 举报
回复
你的数据是怎么绑定上去的?
ajianchina 2015-08-27
  • 打赏
  • 举报
回复
被绑定了数据源的DataGridView不能这样动态新增行的,数据源中新增后重新绑定。
全栈极简 2015-08-27
  • 打赏
  • 举报
回复
你这段代码我刚试过了,没有报错。 其实更直观的写法是操作DataTable,给DataTable增加行。然后直接绑定datagridview1即可。
加载更多回复(3)

111,129

社区成员

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

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

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