Solved

DataGridViewAutoFilter filter box click vs column header click

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…

830 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