Geekpedia Forums Logo

C# - Creating a Dynamic Object

by Adam Livesley on Monday, June 19th - 6:48 PM



Hello,

Would anyone be able to help me as im kind of stuck. My plans are too populate a data grid with data from an object. The object will be populated from a Query, which I dont no as it will be dynamic - thats why im not just creating a connection, and using the data source!

So iv managed too pull data from my database, but now Im having trouble wondering how I can get data from my database into the data grid!



Any help would be appriated!



adam

Hello Adam,

Does your object implement IEnumerable? If it does implement this interface, you'll be able to use the typical DataBind() method.

What methods does your object expose? Does it have a method such as Read() that will allow you to iterate through its values?

Yes, it has a Read() Method, which i can run through each row in the resultset. I just have no idea how to populate an object, based on the current field and the data such as: RowObject --> FieldName = FieldValue



Thanks



adam

You should be able to loop and retrieve the data using something such as:

while(theObject.Read())
{
    Response.Write(theObject["ColumnName"]);
}

What you'll want to do inside the while loop is to create a new DataGrid row, set the values for the cells and add it to your DataGrid. Here are more details: http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_21507588.html

However, if you don't plan to use the DataGrid specific features, you're better of using a Table object (typical HTML table) and create new rows. It should be easier.

I decided in the end to use a library i made called Dynamic Object, it allows me too generate members for objects dynamically and then later pull them out of the object using a foreach loop without ever knowing the member names. Only problem is the object doesnt actually exist, so it would be impossible to apply methods too the objects, enless the contrainer object extended from a code based object ...

Thanks for the help!