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)

...全文
717 23 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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)
DataGridView打印控件5.3版 窗口程序使用演示 包含多个打印控件在窗口程序使用的实例及源代码(有C#的,也有VB的),有源代码 打印控件 实际的打印控件(含.NET4.0的专用打印控件) 网页中使用演示 打印控件在网页中使用的演示程序(有源代码) 1、DGVPrint和VB2008Print 组件在分组打印时,可以在标题中打印当前组名(使用变量“$组名$”); 2、对DGVPrint组件的打印参数设置界面进了重新排版,并去掉了说明页; 3、增加了DrawTitle和DrawTitleEx函数,方便打印标题; 4、打印DGV表格时单元格的边距完全按DGV本身的设置进打印(原来只能按DGV的DefaultCellStyle.Padding设置的大小打印); 5、DGVPrint组件增加了多个属性用于控制弹出的打印参数设置窗口中允许进更改的项(请参数以EnableChange开头的属性,有9个); 6、解决在64位操作系统下无法弹出打印对话框的问题,去掉了打印处理进度窗口中的“.NET轻松打印控件”字样; 7、彻底解决了调用Excel后(导出为excel文件)Excel进程未关闭问题; 8、多表头组件MulHeaderDataGridView具有单元格内容合并显示功能; 9、新增DrawExpression函数,结合ExpressionDrawer类可以很方便对单变量表达式进绘图打印 10、增加了网中使用的打印控件WebPrintServer的水印打印功能和装订线打印功能 11、增加了将报表文件(you结尾的文件)和报表模板生成的报表(用EasyReport组件的GetReportString方法得到的字符串)添加到VB2008Print组件的当前打印作业中的方法(详见AddReport和AddReportByString方法) 12、删除了PrintDataGrid函数(若要打印DataGrid,请先调用ConvertDataGridToDGV函数将DataGrid转换为DataGridView再以打印DGV的形式打印)和ChartGraph图表组件(图表打印请使用功能更强大且不依赖Excel的Chartlet图表组件) 13、其他一些完善,比如对DrawTextEx函数进了完善,解决了有时出现“同一依赖程序集的不同版本之间出现冲突”警告的问题(如果出现,这个问题也并不影响程序的编译和运,您不用太在意),如果电脑上未安装打印机驱动,程序会给出友好提示而不是像以前一样报错,对部分帮助文件内容进了完善(如PrintDGV等函数)。 使用说明:只需要把dll引用到项目、添加打印事件 Private Sub Btn_Printer_Click(sender As System.Object, e As System.EventArgs) Handles Btn_Printer.Click '调用打印 DgVprint1.Alignment = StringAlignment.Center '表格居中 DgVprint1.MainTitle = "监控系统报表记录打印" DgVprint1.SubTitle = "苏州市华工照明科技有限公司" DgVprint1.TableHeaderLeft = "编制单位:路灯管理所" DgVprint1.TableFooterLeft = "制表:苏州市华工照明科技有限公司" DgVprint1.PrintType = VBprinter.DGVprint.mytype.GeneralPrint '在此还可以设置其他属性,当然,也可以DGVPRINT1的悔改窗口中进设置,效果完全一样的 DgVprint1.Print(Me.DataGridView, False) End Sub 以下为导出excel方法实例: Private Sub BtnToExcel_Click(sender As System.Object, e As System.EventArgs) Handles BtnToExcel.Click 'Dim To_Excel As New To_Excel(Me.DataGridView) '方法一:导出excel Dim ToExcel As New VBprinter.DGVprint '方法二:导出excel ToExcel.ExportDGVToExcel(Me.DataGridView) ToExcel.Dispose() End Sub

111,093

社区成员

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

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

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