Get a list of fields from object 3.0.4

Get a list of fields from object 3.0.4

Old forum URL: forums.lhotka.net/forums/t/6082.aspx


esteban404 posted on Monday, January 05, 2009

I'm writing a WinForms project. I have a user control with a grid (DevExpress XtraGrid) in the UI whose column visibility need to change to accommodate the business object content, rules and job requirements. To hide the columns, I need to get the public field names from the object and hide them. I haven't found what I'm looking for yet. Do I add a list to contain the names I need or is there an existing way to get it from the object itself? I think there is in later versions, but I'm not able to use the latest version.

I'm thinking of something like this where the column Caption is the same as the field name using an object data source:
for (int x = 0; x {
if (myList.Contains(this.gridView1.Columns[x].Caption))
this.gridView1.Columns[x].Visible = true;
else
this.gridView1.Columns[x].Visible = false;
}

On second thought, I may need an object for each job type so that live editing can be done on the form, binding each grid to this generic data source object.

Doing that, seems to cause a chicken/egg problem: one object data source for form design and the data source for that design source would change making some design fields inaccessible possibly causing the app to complain violently. There are currently 14 distinct business cases and distinctive UI needs for the data. I have all the shared fields in a base class and that's working fine. Now the UI needs to be reconciled.

Thanks in advance for any suggestions,

_E

RockfordLhotka replied on Tuesday, January 06, 2009

You can use reflection to get a list of properties or fields in an object. CSLA does this in UndoableBase in 3.0, and that might give you some ideas.

esteban404 replied on Wednesday, January 07, 2009

That's perfect, just renamed my column names to the field names and bingo! I knew I'd seen something.

Thanks!

foreach (FieldInfo field in fields)
{
if (this.gridView1.Columns.Contains(this.gridView1.Columns.ColumnByName(field.Name)))
this.gridView1.Columns.ColumnByName(field.Name).Visible = true;
}

Copyright (c) Marimer LLC