Winform程序在执行完方法之后报错

Brilliant丶H 2019-10-10 01:49:02
错误信息如下
“MoenReconciliation.vshost.exe”(CLR v4.0.30319: MoenReconciliation.vshost.exe): 已加载“C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Transactions.resources\v4.0_4.0.0.0_zh-Hans_b77a5c561934e089\System.Transactions.resources.dll”。模块已生成,不包含符号。
System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>未处理的异常</Description><AppDomain>MoenReconciliation.vshost.exe</AppDomain><Exception><ExceptionType>System.NullReferenceException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>未将对象引用设置到对象的实例。</Message><StackTrace> 在 System.Windows.Forms.DataGridViewButtonCell.PaintPrivate(Graphics g, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates elementState, Object formattedValue, String errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts, Boolean computeContentBounds, Boolean computeErrorIconBounds, Boolean paint)
在 System.Windows.Forms.DataGridViewButtonCell.Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates elementState, Object value, Object formattedValue, String errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
在 System.Windows.Forms.DataGridViewCell.PaintWork(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates cellState, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
在 System.Windows.Forms.DataGridViewRow.PaintCells(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow, DataGridViewPaintParts paintParts)
在 System.Windows.Forms.DataGridViewRow.Paint(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow)
在 System.Windows.Forms.DataGridView.PaintRows(Graphics g, Rectangle boundingRect, Rectangle clipRect, Boolean singleHorizontalBorderAdded)
在 System.Windows.Forms.DataGridView.PaintGrid(Graphics g, Rectangle gridBounds, Rectangle clipRect, Boolean singleVerticalBorderAdded, Boolean singleHorizontalBorderAdded)
在 System.Windows.Forms.DataGridView.OnPaint(PaintEventArgs e)
在 System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
在 System.Windows.Forms.Control.WmPaint(Message&amp; m)
在 System.Windows.Forms.Control.WndProc(Message&amp; m)
在 System.Windows.Forms.DataGridView.WndProc(Message&amp; m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)</StackTrace><ExceptionString>System.NullReferenceException: 未将对象引用设置到对象的实例。
在 System.Windows.Forms.DataGridViewButtonCell.PaintPrivate(Graphics g, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates elementState, Object formattedValue, String errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts, Boolean computeContentBounds, Boolean computeErrorIconBounds, Boolean paint)
在 System.Windows.Forms.DataGridViewButtonCell.Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates elementState, Object value, Object formattedValue, String errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
在 System.Windows.Forms.DataGridViewCell.PaintWork(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates cellState, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
在 System.Windows.Forms.DataGridViewRow.PaintCells(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow, DataGridViewPaintParts paintParts)
在 System.Windows.Forms.DataGridViewRow.Paint(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow)
在 System.Windows.Forms.DataGridView.PaintRows(Graphics g, Rectangle boundingRect, Rectangle clipRect, Boolean singleHorizontalBorderAdded)
在 System.Windows.Forms.DataGridView.PaintGrid(Graphics g, Rectangle gridBounds, Rectangle clipRect, Boolean singleVerticalBorderAdded, Boolean singleHorizontalBorderAdded)
在 System.Windows.Forms.DataGridView.OnPaint(PaintEventArgs e)
在 System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
在 System.Windows.Forms.Control.WmPaint(Message&amp; m)
在 System.Windows.Forms.Control.WndProc(Message&amp; m)
在 System.Windows.Forms.DataGridView.WndProc(Message&amp; m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)</ExceptionString></Exception></TraceRecord>

新人不知道如何解决,请求各位大哥帮忙看看 如果错误信息不明显 后续可以贴上代码
...全文
123 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Brilliant丶H 2019-10-11
  • 打赏
  • 举报
回复
引用 3 楼 exception92 的回复:
不太明显,dgv有一个列是button列?根据“System.Windows.Forms.DataGridViewButtonCell.PaintPrivat”查询一下呢。
你好, 我描述的也是不太详细, 下面这个是动态删除dgv里的button列, [code=csharppublic void deleteButtonColumn() { if (dateList.DataSource != null) { for (int i = 0; i < this.dateList.Columns.Count; i++) { if (dateList.Columns[i] is DataGridViewButtonColumn) { dateList.Columns.RemoveAt(i); break; } } } }][/code] 下面这个是点击冻结 按钮之后 在dgv中动态添加 button列
public void freezeBatch()
        {
            try
            {
                deleteButtonColumn();
                this.dateList.DataSource = null;
                string sql = "select ID,FROMDATE,TODATE,BATCH_NAME,(case when freezeStatus=0 then '未冻结' when freezeStatus=1 then '已冻结' else '状态错误' end) as freezeStatus  from DATEITEM where freezeStatus=0 order by FROMDATE";
                DataTable dt = SqlLiteHelp.ExecuteDataSet(SqlLiteHelp.GetConnectings(), sql, CommandType.Text).Tables[0];

                DataGridViewButtonColumn btnColumn1 = new DataGridViewButtonColumn();
                btnColumn1.Name = "操作";
                btnColumn1.UseColumnTextForButtonValue = false;
                btnColumn1.DefaultCellStyle.NullValue = "冻结";
                this.dateList.Columns.Add(btnColumn1);
                this.dateList.DataSource = dt;
                this.dateList.Columns["ID"].Visible = false;
                dt.Columns["FROMDATE"].ColumnName = "开始日期";
                dt.Columns["TODATE"].ColumnName = "结束日期";
                dt.Columns["BATCH_NAME"].ColumnName = "所属期间";
                dt.Columns["freezeStatus"].ColumnName = "冻结状态";
                this.dateList.Columns["操作"].Width = 35;
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.Message);
            }
            
        }
下面这个是点击添加button后的方法
private void dateList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if(dateList.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.ColumnIndex != -1)
            {
                batch = dateList.Rows[e.RowIndex].Cells["所属期间"].Value.ToString();
                DataGridViewButtonCell btnCell = (DataGridViewButtonCell)dateList.Rows[e.RowIndex].Cells[e.ColumnIndex];
                if ("冻结".Equals(btnCell.FormattedValue.ToString()))
                {
                    if ("期初".Equals(batch))
                    {
                        if (MessageBox.Show("冻结期初需要确定期初数据导入完毕,是否继续冻结期初?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                        {
                            string sql = "update DATEITEM set freezeStatus=1 where BATCH_NAME='期初'";
                            int res = SqlLiteHelp.ExecuteNonQuery(SqlLiteHelp.GetConnectings(),sql,CommandType.Text);
                            if (res > 0)
                            {
                                MessageBox.Show("更新成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                                freezeBatch();
                            }
                            else
                            {
                                MessageBox.Show("更新出错!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                    }
                    else if(MessageBox.Show("确定要冻结期间:"+batch+"吗?", "冻结", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        Thread thread = new Thread(updateAllDataStatus);
                        thread.Start();
                        //updateAllDataStatus();
                    }
                }
                else if ("解冻".Equals(btnCell.FormattedValue.ToString()))
                {
                    if (MessageBox.Show("确定要解冻期间:" + batch + "吗?", "解冻", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {

                    }
                }
            }
        }
在更新成功之后会再次刷新这个dgv 报错就出现在刷新dgv的过程 但是这个错误不是每次都会出现,而是偶然性的
exception92 2019-10-11
  • 打赏
  • 举报
回复
不太明显,dgv有一个列是button列?根据“System.Windows.Forms.DataGridViewButtonCell.PaintPrivat”查询一下呢。
Brilliant丶H 2019-10-11
  • 打赏
  • 举报
回复
引用 1 楼 正怒月神 的回复:
未将对象引用设置到对象的实例 应该是空异常了。类似于 Null.toString()引发的
你好,报错显示在System.Windows.Forms.DataGridViewButtonCell.Paint这个方法出错,我的代码报错的地方是窗体的ShowDialog()方法那显示的 详细的内容是 在一个窗体里有DataGridView,窗体中有三个按钮 分别是 冻结 查询 新建期间 使用到DataGridView的是 冻结 和 查询 点击冻结按钮后动态给这个数据表格中添加了DataGridViewButtonColumn, 选中其中一条数据然后点击所在行的按钮去更新数据,数据更新完之后会先弹窗提示更新成功,然后再刷新一下DataGridView(此时含有DataGridViewButtonColumn), 就是在这个再刷新的时候 会有一定几率报错 然后显示报错的地方是这个窗体的ShowDialog() 语句 所以搞得我郁闷的很
正怒月神 2019-10-10
  • 打赏
  • 举报
回复
未将对象引用设置到对象的实例 应该是空异常了。类似于 Null.toString()引发的

110,538

社区成员

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

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

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