Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, September 6, 2011 6:24 PM
I have looked over the Building a Drop-Down Filter List for a DataGridView Column Header Cell and countless examples , but can't seem to find the way to do this.
I am doing dataGridView1.DataSource = myDataTable;
The data table is being loaded from a delimited file and the amount of columns can vary. I'd like the DataGridView's header row to be a ComboBox. The ComboBoxes will be filled from an enum list.
Is there a way to update dataGridView1 to have a ComboBox header row once it's filled with data or a way to make all the column headers for any data source it has a ComboBox?
Thanks in advance,
Scott
All replies (6)
Friday, September 9, 2011 4:33 AM âś…Answered
I've looked into the DataGridViewAutoFilter but I only need the combobox part. It also auto fills the combobox from data in the column, but I need to fill my combobox with my list of options. I do not want it to filter or sort, I just want the user to be able to give the column a header based on one of my options.
Hi Scott,
To create custom Column Header Cell, you need to create a custom class derived from the DataGridViewColumnHeaderCell Class (System.Windows.Forms).
The article demonstrated the implementation details of the DataGridViewAutoFilterColumnHeaderCell class. Please refer to the section for detailed instruction:
Section: DataGridViewAutoFilterColumnHeaderCell Class Implementation Details
The <tt>DataGridViewAutoFilter</tt> library might meet your requirements as it is, making it unnecessary to know any of the implementation details. However, you might want to customize or extend its capabilities to better serve your needs. Understanding the implementation will help if you want to create other custom header cell types for the DataGridView control.
* *
The primary class in the <tt>DataGridViewAutoFilter</tt> library is <tt>DataGridViewAutoFilterColumnHeaderCell</tt> class. This class derives from the DataGridViewColumnHeaderCell class.
* *
The <tt>DataGridViewAutoFilterColumnHeaderCell</tt> implementation details can be divided into four categories:
- Initialization.
- Displaying the drop-down button.
- Displaying, hiding, and handling user interaction with the drop-down list.
- Filtering the bound data when the user selects a filter from the list.
I summarize some important parts as below to understand well the feature implementation.
1) Replace the default DataGridViewColumnHeaderCell with your custom/derived DataGridViewColumnHeaderCell instance.
In the BindingContextChanged event handler, set the DataGridViewColumn.HeaderCell property for the columns you want to affect.
The following example code iterates through the columns in a DataGridView control and sets each header to a new DataGridViewAutoFilterColumnHeaderCell object.
* *
private void dataGridView1_BindingContextChanged(object sender, EventArgs e)
{
* if (dataGridView1.DataSource == null) return;*
* foreach (DataGridViewColumn col in dataGridView1.Columns)*
* {*
* col.HeaderCell = new DataGridViewAutoFilterColumnHeaderCell(col.HeaderCell);*
* }*
* dataGridView1.AutoResizeColumns();*
}
**2) ** col.HeaderCell = new DataGridViewAutoFilterColumnHeaderCell(col.HeaderCell);
The <tt>DataGridViewAutoFilterColumnHeaderCell</tt> class provides a constructor overload that takes an existing DataGridViewColumnHeaderCell instance. The constructor overload copies the property values of the specified cell to the new instance. This is useful when you want to replace an existing header cell with an AutoFilter cell, but keep all other header details the same.
*3) * The derived DataGridViewColumnHeaderCell class implement the <tt>ShowDropDownList</tt> method to display the drop-down list.
** **
To fill the dropDownListBox with your list of options, you can pass your options array (e.g. String[] filterArray) to the <tt>ShowDropDownList</tt> method (add a parameter of String Array type).
* *
public void ShowDropDownList()
{
PopulateFilters();
String[] filterArray = new String[filters.Count];
filters.Keys.CopyTo(filterArray, 0);
dropDownListBox.Items.Clear();
dropDownListBox.Items.AddRange(filterArray);
dropDownListBox.SelectedItem = selectedFilterValue;
HandleDropDownListBoxEvents();
SetDropDownListBoxBounds();
dropDownListBox.Visible = true;
dropDownListBoxShowing = true;
this.DataGridView.Controls.Add(dropDownListBox);
dropDownListBox.Focus();
// Invalidate the cell so that the drop-down button will repaint
// in the pressed state.
this.DataGridView.InvalidateCell(this);
}
4) Here the dropDownListBox is just a ListBox control as a member of the derived DataGridViewColumnHeaderCell class.
The primary initialization task is to create a ListBox control for use as a drop-down list. The <tt>DataGridViewAutoFilter</tt> library uses a single ListBox-derived control for all AutoFilter header cells in an application, storing it in a static variable called <tt>dropDownListBox</tt>. Using a single instance is possible because only one drop-down list is displayed at a time
Martin Xie [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Wednesday, September 7, 2011 9:04 AM
Have you tried this?
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn.headertext.aspx
chanmm
chanmm
Wednesday, September 7, 2011 9:44 AM
I have looked over the Building a Drop-Down Filter List for a DataGridView Column Header Cell and countless examples , but can't seem to find the way to do this.
Hi Scott,
Welcome to MSDN Forum.
According to the article instruction, such feature is implemented by using the <tt>DataGridViewAutoFilter</tt> library. Please double check.
To add the AutoFilter feature to your application using the designer:
- In your Windows Application project, add a reference to the DataGridViewAutoFilter.dll assembly.
The sample DataGridViewAutoFilter library depends on four things:
- The DataGridView.DataSource must be set to a BindingSource component.
- The BindingSource component must be bound to an IBindingListView implementation.
- The IBindingListView.SupportsFiltering property value must be true.
- The IBindingListView.Filter property must support multi-column filtering.
The DataGridViewAutoFilter Class Library
Using the DataGridViewAutoFilter Library
Martin Xie [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Wednesday, September 7, 2011 3:45 PM
I've looked into the DataGridViewAutoFilter but I only need the combobox part. It also auto fills the combobox from data in the column, but I need to fill my combobox with my list of options. I do not want it to filter or sort, I just want the user to be able to give the column a header based on one of my options.
The columns will contain user information and the user will be able to select which column is first name, last name, address, city, etc.
Tuesday, August 20, 2013 10:33 AM
Hi,
Is it possible to give detailed steps to follow to have just a combobox without filter ?
Thank you.
Monday, December 2, 2013 12:28 PM
Hi Sara-an,
I might be too late, but I went to the link which Scott_P had mentioned, and downloaded the VS 2005 code, converted it to VS 2013, luckily everything worked fine. Inorder to avoid the filtering you can comment out the UpdateFilter() method of the DataGridViewAutoFilterColumnHeaderCell class wherever it is used in that Solution, and retrieve the selected combo box value by accessing the dropDownListBox.SelectedItem.ToString().Equals(selectedFilterValue) . This gives you the chosen value.
Happy to see some old code helping all the way for newbies...
Kind Regards,
Mafaz