C# anonymous type foreach looping
Asked Answered
V

2

1

I need to loop through the properties of a custom object type that I'm getting back from the database and only show the columns that contain data. This means I cannot simply bind the list of objects to the datagrid. I don't want to loop through each object and see if the column is empty/null and determine in the UI to display it. What I'm thinking is in my business layer before I send the object back I would send an IEnumerable back with only those columns that should be visible. Thus I was thinking of using Linq to Object to do this, but I'm not sure that would be very pretty.

Does anyone know of a solution that I could use without a ton of IF statements that I could do to check through a large object (30 or so columns) to determine what should be shown or not.

Foreach (CustomerData customerdata in Customers) 
{ 
    if (!customerdata.address.Equals("")) 
       {
            dgvCustomerData.Column["Address"].visible = false;
         }
        //Continue checking other data columns...
}

I wish to avoid all of this in the UI and all the IFs... I'm having a brain fart on this one can anyone help me?

Thanks

Venose answered 10/12, 2009 at 18:13 Comment(0)
E
2

Take a look at the .NET Reflection Libraries. You can use reflection to get ahold of all of an object's properties, and loop through them to find out if they are null or not. Then you could return a collection of KeyValuePair objects where Key = property name, and Value = true/false. You'd then use the keyvaluepairs to set column visibility...

Empire answered 10/12, 2009 at 18:20 Comment(1)
This was more of the solution I was thinking, but instead of a hashtable, I was thinking more of an IEnumerable list that I could then simply bind to the datagrid. I will research this a bit more.Venose
I
4

You could do the following to simplify it a bit

Action<T,string> del = (value,name) => {
  if ( value.Equals("") ) {
    dgvCustomerData.Column[name].Visible = false;
  }
};
foreach ( var data in Customers ) {
  del(data.address,"Address");
  del(data.name, "Name");
  ...
}
Illuminati answered 10/12, 2009 at 18:19 Comment(1)
I'd like to pull this out of the UI layer so that the UI doesn't have to decide what to show or not. However, this was the approach I was thinking of doing in the first place.Venose
E
2

Take a look at the .NET Reflection Libraries. You can use reflection to get ahold of all of an object's properties, and loop through them to find out if they are null or not. Then you could return a collection of KeyValuePair objects where Key = property name, and Value = true/false. You'd then use the keyvaluepairs to set column visibility...

Empire answered 10/12, 2009 at 18:20 Comment(1)
This was more of the solution I was thinking, but instead of a hashtable, I was thinking more of an IEnumerable list that I could then simply bind to the datagrid. I will research this a bit more.Venose

© 2022 - 2024 — McMap. All rights reserved.