Search code examples
c#winformsbindingbindinglistgridcontrol

How to display selective items in a binding list in grid control


I have implemented below code:

gridControl.DataSource = CusColumnList

CusColumnList is of type MyBindingList which inherits BindingList, in my case T is class MyColumn. The binding works well.

But now my problem comes, I don't want the data source to bind to every column in CusColumnList, I only want it binds to column whose name contains "ABC" or whose display name contains "XYZ". I tried to set

gridControl.DataSource = CusColumnList.Where(column => column.Name.Contains("ABC") || column.DisplayName.Contains("XYZ"));

But seems it does not work. I also tried to create another bindinglist collection MyTempCusColumnList of type MyBindingList, and in the Get method of this MyTempCusColumnList, I just return every item in CusColumnList where the name or display name qualifies. But in this way, every time when CusColumnList is updated, I need to manually update MyTempCusColumnList.

I wonder whether there is a better way to archive this goal with just CusColumnList. Thanks!

Edit : format code


Solution

  • You could use a filter string on a BindingSource object.
    Check out the MSDN documentation on it, it's quite good: http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.filter(v=vs.100).aspx