Solved

DataGridViewAutoFilter filter box click vs column header click

Posted on 2013-05-21
3
4,952 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
visual Basic Net vs c# net 8 54
.Net application crashing 6 20
cs.Designer Issue(2) 2 21
Round up to 100% in .NET 10 44
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 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

13 Experts available now in Live!

Get 1:1 Help Now