reflection + get property of a class which is property of another class

  • Thread starter Thread starter Spunny
  • Start date Start date
S

Spunny

Guest
Hi,

We are trying to export telerik radgrid rows to excel. We are looping through rows and trying to get columns. For this reflection is used. Below is the code

Grid datasource is an object with one of the property being another class like this:

eg:


public class summary
{

private int _trnID = 0;
private Client _clientDetails;

public int TrnID
{
get{ return _trnID;}
set{ _trnID = value;}
}

public Client ClientDetails
{
get{
return _clientDetails;
}
set{ _clientDetails;= value;}
}

}

public class Client
{
private short _clientID;
private string _name;

public short ClientID
{
get { return _clientID; }
set { _clientID = value; }
}

public string Name
{
get { return _name; }
set { _name = value; }
}
}

This class summary gets populated from database and used as datasource for grid.
Like this:
<telerik:GridBoundColumn DataField="TrnID" DataType="System.Int32" HeaderText="ID" UniqueName="TrnID"

HeaderTooltip="Transaction ID" />
<telerik:GridBoundColumn DataField="ClientDetails.Name" HeaderText="Client" UniqueName="ClientDetails.Name" HeaderTooltip="Client" HeaderStyle-Width="6%" />

When I am exporting to excel, this is what I am doing:

protected void CreateExcelWorkBook(RadGrid pRadGrid)
{
List<PropertyInfo> properties = new List<PropertyInfo>();
int cellIdx = 0;

for (int colIdx = 0; colIdx < pRadGrid.Columns.Count; colIdx++)
{
GridColumn gridCol = pRadGrid.Columns[colIdx];
if (gridCol.Visible && gridCol.Exportable && gridCol.Display)
{
properties.Add(typeof(T).GetProperty(((GridBoundColumn)gridCol).DataField));
}
}
}
Above code properties.Add(typeof(T).GetProperty(((GridBoundColumn)gridCol).DataField)); works fine for TrnID property and I get TrnID as propertyname.
When it loops through and goes to ClientDetails property, it is null becasue unlike 'TrnID', it is 'ClientDetails.Name'.
How can I get property of object with in object.

Thank You

Continue reading...
 
Back
Top