how i can show object in gridview or in any usful contro
public class EventDetail
{
private Int64 logID;
public Int64 LogID
{
get { return logID; }
set { logID = value; }
}
private Object logedObject;
public Object LogedObject
{
get { return logedObject; }开发者_StackOverflow社区
set { logedObject = value; }
}
}
First off, you dont need to use fields if you do nothing in the property other then set it.
public Object LogedObject { get; set; }
is enough.
Secondly, to show a list of your objects in a DataGrid, as example.
protected void Page_Load(object sender, EventArgs e)
{
DataGrid dg = new DataGrid();
dg.DataSource = getModels();
dg.DataBind();
}
public List<EventDetail> getModels()
{
var m = new List<EventDetail>();
for (int a = 0; a < 15; a++)
{
m.Add(new EventDetail() { prop1 = a, prop2 = string.Format("Prop2 {0}", a) });
}
return m;
}
public class EventDetail
{
public Int64 LogID { get; set; }
public Object LogedObject { get; set; }
}
Look at the DataBinder class, it should allow you to do what you want ;-)
To bind with GridView (or similar controls such as Repeater, ListView), you typically need to know property names of object because that's what you will use to bind column to. So is this example, you may bind to LogID and LogedObject properties. For later, ToString will be invoked on object to show the string representation.
Instead of returning collection of class EventDetail, if you only have to bind logedevent, then do the following. Extract the collection of LogedObject in some other collection or
directly bind it to grid
protected void BindGrid()
{
gv.DataSource = EventDetail_Object.LogedObect_Property_Of_Class;
gv.DataBind();
}
tanx for any body help me i solve this problem by reflection i use refelection to get my properties of my object an thie values an then add this value to my string list
//exteract properties of loged object
PropertyInfo[] _PropertyInfo = _ObjectType.GetProperties();
List<string> _ObjBeforTostring = new List<string>();
//_ObjBeforTostring.Add("");
_ObjBeforTostring.Add("*************Befor Object**********");
_ObjBeforTostring.Add("");
foreach (PropertyInfo pf in _PropertyInfo)
{
if (_objbefor != null)
{
string _str = pf.GetValue(_objbefor, null).ToString();
_ObjBeforTostring.Add(pf.Name.ToString() + " :: ( " + _str + " )");
_ObjBeforTostring.Add("==============================");
}
}
_ObjBeforTostring.Add("");
_ObjBeforTostring.Add("*************After Object**********");
_ObjBeforTostring.Add("");
foreach (PropertyInfo pf in _PropertyInfo)
{
if (_objAfter != null)
{
string _str = pf.GetValue(_objAfter, null).ToString();
_ObjBeforTostring.Add(pf.Name.ToString() + " :: ( " + _str+" )");
_ObjBeforTostring.Add("==============================");
}
}
精彩评论