Filtering DataGridView
I am creating a control that should be able to use any list. Essentially the following code:
void BindData(IList list)
{
BindingSource bs = new BindindSource();
bs.DataSource = list;
this.DataGridView.DataSource = bs;
}
Now I have a textbox that I want to use to filter the data in my grid. I figured it would be as easy as setting the bs.Filter property, but apparently not. Bs.SupportsFiltering also returns false.
Is this a problem with me using IList? If so, is there another collection class / interface that I can use to achieve the same effect? (Again, I'm not sure if this is the type of objects in the list.
+2
source to share
2 answers
Not knowing the type that is being passed to me, I led to filtering the data manually. Here is my code snippet. It works well. Hopefully it doesn't turn out to be too slow with a lot of data. :: Fingers crossed ::
List<object> filteredData = new List<object>();
foreach (object data in this.DataSource)
{
foreach (var column in this.Columns)
{
var value = data.GetType().GetProperty(column.Field).GetValue(data,null)
.ToString();
if (value.Contains(this.ddFind.Text))
{
filteredData.Add(data);
break;
}
}
}
this.ddGrid.DataSource = filteredData;
+4
source to share