-1

我的 winform 应用程序有两个选项卡和多线程,一个是主选项卡,另一个是日志选项卡。我只使用日志选项卡在 datagridview 控件中显示日志。单击日志选项卡时随机发生异常(未单击行或列),我尝试过但无论如何都找不到修复它。这是错误日志:

Message : Object reference not set to an instance of an object.
Source : System.Windows.Forms
TargetSite : System.Windows.Forms.DataGridViewElementStates GetRowState(Int32)
StackTrace :    at System.Windows.Forms.DataGridViewRowCollection.GetRowState(Int32 rowIndex)
   at System.Windows.Forms.DataGridView.ComputeHeightOfFittingTrailingScrollingRows(Int32 totalVisibleFrozenHeight)
   at System.Windows.Forms.DataGridView.GetOutOfBoundCorrectedHitTestInfo(HitTestInfo& hti, Int32& mouseX, Int32& mouseY, Int32& xOffset, Int32& yOffset)
   at System.Windows.Forms.DataGridView.OnMouseMove(MouseEventArgs e)
   at System.Windows.Forms.Control.WmMouseMove(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.DataGridView.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Message : Object reference not set to an instance of an object.
Source : System.Windows.Forms
TargetSite : Void ClearInternal(Boolean)
StackTrace :    at System.Windows.Forms.DataGridViewRowCollection.ClearInternal(Boolean recreateNewRow)
   at System.Windows.Forms.DataGridView.OnClearingColumns()
   at System.Windows.Forms.DataGridViewColumnCollection.Clear()
   at System.Windows.Forms.DataGridView.Dispose(Boolean disposing)
   at System.ComponentModel.Component.Dispose()
   at System.Windows.Forms.Control.Dispose(Boolean disposing)
   at System.ComponentModel.Component.Dispose()
   at System.Windows.Forms.Control.Dispose(Boolean disposing)
   at System.Windows.Forms.TabControl.Dispose(Boolean disposing)
   at System.ComponentModel.Component.Dispose()
   at System.Windows.Forms.Control.Dispose(Boolean disposing)
   at System.Windows.Forms.Form.Dispose(Boolean disposing)

更新状态码:

    private void updateMessage(int index, string message)
    {
        try
        {
            this.dgForums.Rows[index].Cells["ColStatus"].Value = message;

            System.Windows.Forms.Application.DoEvents();
        }
        catch
        {
        }
    }
4

1 回答 1

0

当您单击除网格单击事件或任何其他与事件相关的网格中的任何行和列之外的网格时,在您的代码之前添加代码

if(this.dgForums.Rows.Count > 0)
{
  //Continue your code here
}
于 2012-10-20T04:57:54.720 回答