I need to sort and filter a List that is loaded to a DataGridView

rickm

New member
Joined
Oct 21, 2020
Messages
2
Programming Experience
10+
Just getting back into the swing of things and trying not to develop from bad habits.

I have created a new project that currently reads in JSON via WebClient.DownloadString. The class identifies the items by name and this is stored in a List with this
C#:
List<Member> members = JsonConvert.DesearlizeObject<List<Member>>(json);
I then make the DataSource connection.
C#:
dataGridView1.DataSource = members;
Everything displays as expected.

Now I need to have the ability to Sort and Filter the datagrid.

I click on a column header which triggers the following:
C#:
private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {

            if (dataGridView1.Columns[e.ColumnIndex].HeaderText == "State")
            {
                dataGridView1.Sort(dataGridView1.Columns["State"], ListSortDirection.Ascending);

            }
        }
The error that is returned says:

"DataGridView control must be bound to an IBindingList object to be sorted."

Where do I look for examples of doing this?

Is there a better way of achieving this without using a DataGridView?

Thank you,
 
Last edited by a moderator:
For most cases, the out of the box BindingSource class provides a ready made implementation of IBindingList that handles most of the common things you would do with something that requires an IBindingList.


and there is also the BindingList<T>:

All of these you could have found if you had taken time to look at the documentation for IBindingList.
 
RTFM didn't help.

I'll keep digging for the appropriate way to do this.

Pointers that mentioned using a WebClient to bring in JSON into an appropriate object would have been a nice answer.

Thanks,
Rick
 
RTFM didn't help.
The thing is, it should. You can't always solve your problem completely by doing so but you can usually get much further along. No one wants to discourage people from using sites like this one. We wouldn't be here answering questions if we did. What we want is for people to use them appropriately, i.e. do all you can for yourself first and then ask about the parts you can't do for yourself. If you get an error message then the very first thing you should be doing is searching the web for that error message. If you are referred to a type or member by an error message or someone else, the very first thing you should be doing is reading the documentation for that type or member. You can then gather all the relevant information you can, make all the sense of it you can, make all the relevant changes to your code that you can and then ask us more specific questions on the parts you still can't fix. If you get an error message and then stop everything to ask us, you definitely haven't done all you can. We want to help but the reason we want to help is that we want to see people become the best developers they can be. That doesn't happen if we gift-wrap things that you should be looking for and finding for yourself.
 
Pointers that mentioned using a WebClient to bring in JSON into an appropriate object would have been a nice answer.
What would that have to do with sorting and filtering in the DataGridView and the error that you reported regarding an IBindingList?
 
Back
Top Bottom