【问题标题】:Exception when click DataGridview tab c#, .Net 4.0单击 DataGridview 选项卡 c#、.Net 4.0 时出现异常
【发布时间】:2012-10-20 03:44:00
【问题描述】:

我的 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
        {
        }
    }

【问题讨论】:

  • 在标签点击中告诉我们你在做什么..
  • 我只使用数据网格来显示日志。我更新的代码

标签: c# winforms tabs


【解决方案1】:

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

if(this.dgForums.Rows.Count > 0)
{
  //Continue your code here
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-17
    • 1970-01-01
    • 1970-01-01
    • 2011-11-26
    • 1970-01-01
    • 1970-01-01
    • 2012-09-08
    • 1970-01-01
    相关资源
    最近更新 更多