DataGridView 中的 DataGridViewAutoFilter 引发 null 引用异常

DataGridViewAutoFilter in DataGridView throwing null reference exception

提问人:Srijani Ghosh 提问时间:1/6/2017 最后编辑:Srijani Ghosh 更新时间:1/6/2017 访问量:930

问:

我正在尝试使用DataGridViewAutoFilter.dll在Windows窗体应用程序内的数据网格视图中创建像excel这样的自动过滤器。

下面是 datagridview 的代码:

 // 
// dataGridView1
// 
this.dataGridView1.AllowUserToAddRows = false;
this.dataGridView1.AllowUserToDeleteRows = false;
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.file,
this.line,
this.message});
this.dataGridView1.Enabled = false;
this.dataGridView1.Location = new System.Drawing.Point(16, 473);
this.dataGridView1.MultiSelect = false;
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.ReadOnly = true;
this.dataGridView1.RowHeadersVisible = false;
this.dataGridView1.Size = new System.Drawing.Size(700, 278);
this.dataGridView1.TabIndex = 15;
this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClick_1);
this.dataGridView1.CellDoubleClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellDoubleClick);
this.dataGridView1.CellMouseDown += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.dataGridView1_CellMouseDown);
this.dataGridView1.CellMouseEnter += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClick);
this.dataGridView1.KeyDown += new KeyEventHandler(dataGridView1_KeyDown);

// 
// file
// 
this.file.HeaderText = "File";
this.file.Name = "file";
this.file.ReadOnly = true;
// 
// line
// 
this.line.HeaderText = "Line";
this.line.Name = "line";
this.line.ReadOnly = true;
// 
// message
// 
this.message.HeaderText = "Message";
this.message.Name = "message";
this.message.ReadOnly = true;

void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Alt && (e.KeyCode == Keys.Down || e.KeyCode == Keys.Up))
            {
                DataGridViewAutoFilterColumnHeaderCell filterCell =
                    this.dataGridView1.CurrentCell.OwningColumn.HeaderCell as
                    DataGridViewAutoFilterColumnHeaderCell;
                if (filterCell != null)
                {
                    filterCell.ShowDropDownList();
                    e.Handled = true;
                }
            }
        }

我有::

使用 UI 向 DataGridView 添加了列 添加

DataGridViewAutoFilter.dll 程序集使用 NuGet 添加到项目中。

在为 DataGridView 创建列时,我选择了 DataGridViewAutoFilterTextBoxColumn 作为列类型。

未将 DataTable 用于任何数据绑定。取而代之的是,我使用以下方法动态添加行:

 String key = entry.Key;
ArrayList messages = entry.Value;
if (null != key && null != messages)
{
    for (int i = 0; i < messages.Count; i++)
    {
        ArrayList smallList = (ArrayList)messages[i];
        if (null != smallList)
            dataGridView1.Rows.Add(key, smallList[0] + "", smallList[1] + "");
    }
}

当我运行我的项目时,我能够在标题文本旁边看到下拉符号,但是当我单击下拉符号时,它给出错误::

Error screenshot.. entry point for this whole project

此错误的内部日志:

System.NullReferenceException was unhandled
  Message=Object reference not set to an instance of an object.
  Source=DataGridViewAutoFilter
  StackTrace:
       at DataGridViewAutoFilter.DataGridViewAutoFilterColumnHeaderCell.PopulateFilters()
       at DataGridViewAutoFilter.DataGridViewAutoFilterColumnHeaderCell.ShowDropDownList()
       at DataGridViewAutoFilter.DataGridViewAutoFilterColumnHeaderCell.OnMouseDown(DataGridViewCellMouseEventArgs e)
       at System.Windows.Forms.DataGridViewCell.OnMouseDownInternal(DataGridViewCellMouseEventArgs e)
       at System.Windows.Forms.DataGridView.OnCellMouseDown(DataGridViewCellMouseEventArgs e)
       at System.Windows.Forms.DataGridView.OnMouseDown(MouseEventArgs e)
       at System.Windows.Forms.Control.WmMouseDown(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.DataGridView.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at WindowsFormsApplication1.Program.Main() in c:\Users\srijani.ghosh\Documents\Visual Studio 2012\Projects\SearchKey\WindowsFormsApplication1\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

谁能帮忙,请帮我解决这个问题?

提前致谢!

C# .NET WinForms DataGridView NullReferenceException

评论

1赞 Dave R. 1/6/2017
您似乎对条目、条目没有任何空检查。钥匙或入口。价值。这将是一个很好的起点。这可能是一个问题,而不是 DataGridView 本身的问题。
0赞 Srijani Ghosh 1/6/2017
谢谢你提醒我...更改了代码。但是,问题仍然存在.
1赞 Fabzien 8/28/2017
你发现什么了吗?我有完全相同的问题

答: 暂无答案