Solved

DataGridViewAutoFilter filter box click vs column header click

Posted on 2013-05-21
3
5,478 Views
Last Modified: 2013-05-27
I have some code in my ColumnHeaderMouseClick event for my datagrid to sort programatically among other things.  I am using the DataGridViewAutoFilter.dll for filtering on almost all my datagrid columns, I do not want any of my code in the ColumnHeaderMouseClick to execute if the user was trying to click the little filter arrow.  Here would be the psuedo code I do not know how to write.

private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
  {
   //instantiate DataGridViewAutoFilterColumnHeaderCell
   DataGridViewColumn dc = dataGridView1.Columns[e.ColumnIndex];
   dc.HeaderCell = new DataGridViewAutoFilterColumnHeaderCell(dc.HeaderCell);
  //somehow access the functions in dc, the source code for DataGridViewAutoFilter shows
  //a function that can tell if the little filter box was clicked that they call from the
  //mousdown event
  }

Here is SOME of their code in the dll
            // Show the drop-down list if filtering is enabled and the mouse click occurred
            // within the drop-down button bounds. Otherwise, if sorting is enabled and the
            // click occurred outside the drop-down button bounds, sort by the owning column.
            // The mouse coordinates are relative to the cell bounds, so the cell location
            // and the scrolling offset are needed to determine the client coordinates.
            if (FilteringEnabled &&
                DropDownButtonBounds.Contains(
                e.X + cellBounds.Left - scrollingOffset, e.Y + cellBounds.Top))
            {
                // If the current cell is in edit mode, commit the edit.
                if (this.DataGridView.IsCurrentCellInEditMode)
                {
                    // Commit and end the cell edit.  
                    this.DataGridView.EndEdit();

                    // Commit any change to the underlying data source.
                    BindingSource source =
                        this.DataGridView.DataSource as BindingSource;
                    if (source != null)
                    {
                        source.EndEdit();
                    }
                }
                ShowDropDownList();
            }
0
Comment
Question by:abrusko
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 16

Expert Comment

by:Kalpesh Chhatrala
ID: 39187604
Hello,

Below is Advanced DataGridView with Sorting and Filtering like excel.

http://adgv.codeplex.com/
0
 
LVL 2

Accepted Solution

by:
abrusko earned 0 total points
ID: 39187670
Too bad I didn't know about this before.  Here is what I did to fix what I had in the meantime, ugly but works.

I got the mouse X, Y from mousedown event
    private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
      {
      mousedown_y = e.Y;
      mousedown_x = e.X;
      }  

Then in the column header mouse click
    private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
      {
      DataGridViewAutoFilterColumnHeaderCell filterCell =
        this.dataGridView1.Rows[0].Cells[e.ColumnIndex].OwningColumn.HeaderCell
          as DataGridViewAutoFilterColumnHeaderCell;

      try
        {
        PropertyInfo pInfo =
          filterCell.GetType().GetProperty("DropDownButtonBounds", System.Reflection.BindingFlags.Public |
                                                                 System.Reflection.BindingFlags.NonPublic |
                                                                 System.Reflection.BindingFlags.Instance);
        if (rectangle.Contains(mousedown_x, mousedown_y) == false) //did they click inside the filter or just the column
          { sort_datagrid(e.ColumnIndex); }
        rectangle = (Rectangle)pInfo.GetValue(filterCell, null);
        }
      catch //this isnt a filtered col so just sort
        { sort_datagrid(e.ColumnIndex); }
0
 
LVL 2

Author Closing Comment

by:abrusko
ID: 39198756
Too bad I didn't know about that other datagrid before.  Here is what I did to fix what I had in the meantime, ugly but works.
0

Featured Post

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question