Pieter wrote:
Hi,
I want to bind a proeprty of a child object to a DataGridView, but it
doesn't work.
For instance:
Imagen that I have an object Company, and this object company has a child
object Address.
Address has a property Street.
Now I want to bind a list oc Company objects to my DataGridView, and show
there Street proeprty.
So you would think you would have to add Address.Street as DataPropertyNam e
in the DataGridViewTex tBoxColumn, but this doesn't show the value...
Does anybody knows how to do this?
Thansk a lot in advance,
Pieter
Hi, I think you can bind child property in this view but I don't know
how to edit these property (in your problem)
This is the way: use Repeater (or DataList) and template
In your Visual Studo, open web page containning DataGridView (in Code
mode - not Design mode). May be your code like this
<asp:Repeater Id="companyGrid View"
OnItemDataBound ="companyGridVi ew_ItemDataBoun d">
<HeaderTemplate ><table><tr><td >Company</td></tr></HeaderTemplate>
<FooterTemplate ></table></FooterTemplate>
<ItemTemplate >
<tr><td><asp:La bel Text="<%# Eval("CompanyNa me") %>">
<asp:Repeater Id="addressGrid View">
<HeaderTemplate ><table><tr><td >Address</td></tr></HeaderTemplate>
<FooterTemplate ></table></FooterTemplate>
<ItemTemplate >
<tr><td><asp:La bel Text="<%# Eval("CompanyAd dress") %>">
</td></tr>
</ItemTemplate>
</asp:Repeater>
</td></tr>
</ItemTemplate>
</asp:Repeater>
In your code behind:
protected void companyGridView _ItemDataBound( object sender,
RepeaterItemEve ntArgs e) {
if( e.Item.ItemType == ItemType.Item || e.Item.ItemType ==
ItemType.Altern atingItem) {
Company boundObj = e.Item.DataItem as Company; // your Company
object which is binding
Repeater rpt = e.Item.FindCont rol("addressGri dView") as Repeater; //
ger reference
rpt.DataSource = Companay.Addres s; // Address must be a boundable
object (sorry if I don't use this term exactly) (IList, IEnumberable,
DataTable, ..)
rpt.DataBind();
}
}
The important thing is ItemDataBound event, you registter that event and
do anything you want