Solved

DataGridViewAutoFilter filter box click vs column header click

Posted on 2013-05-21
3
5,152 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
  • 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

816 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now