DataGridView中的DataGridViewAutoFilter抛出null引用异常

时间:2017-01-06 11:23:48

标签: c# .net winforms datagridview nullreferenceexception

我正在尝试使用DataGridViewAutoFilter.dll在Windows窗体应用程序内的datagridview中创建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添加了列

使用NuGet将DataGridViewAutoFilter.dll程序集装配到项目中。

在为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: 

任何人都可以帮忙,请帮我解决这个问题吗?

提前致谢!

0 个答案:

没有答案