I don't see anything wrong with your approach.
In the end, I created a method to convert my object array to a DataSet so
that I could bind to the DataSet in the code, and also declaratively access
the property information from the HTML.
The sample is binding to a DropDown, which is the same as binding to a Grid
except that you have to specify, in the code, which fields represent the
Value and Text for each item. If you were using a DataGrid you could simple
use a BoundColumn to display the property values.
Here is the code I use:
/// <summary>
/// A method to convert an array of objects returned from a web service into
/// a DataSet so that they can be bound declaratively in the HTML. This is
to work
/// around the fact that the default Visual Studio WebServices references
/// deserialize object properties as fields instead of true get/set
properties.
/// </summary>
/// <param name="objectArray">The array of objects.</param>
/// <returns>A DataSet with a table named after the objectType.</returns>
public static DataSet ConvertObjectArrayToDataSet(object objectArray)
{
StringWriter writer = new StringWriter();
XmlSerializer serializer = new XmlSerializer(objectArray.GetType());
serializer.Serialize(writer, objectArray);
DataSet ds = new DataSet();
StringReader reader = new StringReader(writer.ToString());
ds.ReadXml(reader);
return ds;
}
// Here is an example of calling the static method and binding the
// resultant Dataset to a DropDown. The nice thing about this is
// that you can sort it before binding.
DataSet binder = Helpers.ConvertObjectArrayToDataSet(objs);
binder.Tables[0].DefaultView.Sort = "ClientName";
dropDown.DataSource = binder.Tables[0].DefaultView;
dropDown.DataValueField = "ID";
dropDown.DataTextField = "ClientName";
dropDown.DataBind();
"fd537" wrote:
Hi,
I was searching the internet to find out how to bind the selected
columns of an array of custom objects to the datagrid and i found this
post on google groups. I want you to please have a look at my sample
code and let me know if i am on the right track or not. I am new to
..net and would like to have some guidance from u guys. here is what
iam trying to do:
1) get an array of objects from the web service method(for ex: lets
say there are 4 fields/properties for each object)
2) i want to bind only 2 fields/properties to the datagrid.
3) i have written an event handler that assigns the value of the
property to the literal control inside a datagrid's item template
column.
4) attached the event handler to the datagrid by using the
OnItemDataBound event of the datagrid
here is the code that i have:
event handler for the OnItemDataBound event :
public void Item_Bound(object sender, DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
Literal l = (Literal)e.Item.FindControl("lit");
if(l.Text == String.Empty)
{
l.Text = ((FormsOrderingWS.Order)
e.Item.DataItem).OrderDate.ToString();
}
}
}
HTML code for datagrid:
<asp:DataGrid OnItemDataBound="Item_Bound"......>
<asp:Columns>
<asp:TemplateColumn HeaderText="Order Date">
<ItemTemplate>
<asp:Literal ID="lit" Runat="server"></asp:Literal>
</ItemTemplate>
</asp:TemplateColumn>
</asp:Columns>
</asp:DataGrid>
please suggest changes if iam wrong. Thank you