none
autofilter on datagridview headercell

    Question

  • I would like to display the AutoFilter drop-down button in a header cell of the windows's datagridview control.
    This is what I have:

    DataTable dt = new DataTable();
                dt = DataGet();

                if (dt.Rows.Count > 0)
                {
                    DataView defaultView = dt.DefaultView;

                    dgv.DataSource = null;
                    dgv.DataSource = defaultView;
                }


    dgv.Columns["Surnames"].HeaderCell = new DataGridViewAutoFilterColumnHeaderCell();

    This above line gives the following error.
    Do you know how this can be solved please?

    Note that I do not want to create the fields of the datagridview at design time because as you see in this code, they get added automatically to the datagridview.

    Thanks


    {"The DataSource property of the containing DataGridView control must be set to a BindingSource."} System.Exception {System.NotSupportedException}

    • Moved by Leo Liu - MSFT Friday, January 27, 2012 5:40 AM Moved for better support. (From:Visual C# General)
    Thursday, January 26, 2012 4:46 PM

Answers

  • That is the library's check, it do not allow you bind anything except the BindingSource type object to the current DataGridView when you're using the AutoFilter Column type in it.(This is written in the codes, you can download it and view the source code.)

    So you can change your code like this:

                    BindingSource bs = new BindingSource();
                    bs.DataSource = defaultView;
                    dataGridView1.DataSource = bs;
    



    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by arkiboys Friday, January 27, 2012 9:43 AM
    Friday, January 27, 2012 6:00 AM
    Moderator

All replies

  • Hi

    Check below control is as per your requirement

    http://www.codeproject.com/Articles/9947/DataGrid-with-built-in-filter-functionality

     

    HTH

    Rozy

    Thursday, January 26, 2012 6:22 PM
  • That is the library's check, it do not allow you bind anything except the BindingSource type object to the current DataGridView when you're using the AutoFilter Column type in it.(This is written in the codes, you can download it and view the source code.)

    So you can change your code like this:

                    BindingSource bs = new BindingSource();
                    bs.DataSource = defaultView;
                    dataGridView1.DataSource = bs;
    



    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by arkiboys Friday, January 27, 2012 9:43 AM
    Friday, January 27, 2012 6:00 AM
    Moderator
  • Hi

    Check below control is as per your requirement

    http://www.codeproject.com/Articles/9947/DataGrid-with-built-in-filter-functionality

     

    HTH

    Rozy

    Thank you for the link which I have now studied.
    Unfortunately, it does not do what I am asking here.
    Please note that at present, I am populating a datagridview dgv in code using a dataview which is populated from a datatable.
    The dgv is populated at runtime and so are the columns with it but I would like to know how to make one of the columns to have autofilter capability on the headercell just like an excel spreadsheet when you set autofilter?
    Note that I do NOT want to add columns at design time.
    Thanks
    Friday, January 27, 2012 9:26 AM
  • That is the library's check, it do not allow you bind anything except the BindingSource type object to the current DataGridView when you're using the AutoFilter Column type in it.(This is written in the codes, you can download it and view the source code.)

    So you can change your code like this:

     

                    BindingSource bs = new BindingSource();
                    bs.DataSource = defaultView;
                    dataGridView1.DataSource = bs;
    


     


    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us
    Yes, this solved the problem.
    Many thanks
    Friday, January 27, 2012 9:44 AM
  • You're welcome!
    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us
    Tuesday, January 31, 2012 11:57 AM
    Moderator