Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6060
  • Last Modified:

DataGridViewAutoFilter filter box click vs column header click

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
abrusko
Asked:
abrusko
  • 2
1 Solution
 
Kalpesh ChhatralaSoftware ConsultantCommented:
Hello,

Below is Advanced DataGridView with Sorting and Filtering like excel.

http://adgv.codeplex.com/
0
 
abruskoAuthor Commented:
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
 
abruskoAuthor Commented:
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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now