PC Review


Reply
Thread Tools Rate Thread

Bind Property of Child-Objecty to DataGridView

 
 
Pieter
Guest
Posts: n/a
 
      16th Nov 2007
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 DataPropertyName
in the DataGridViewTextBoxColumn, but this doesn't show the value...

Does anybody knows how to do this?


Thansk a lot in advance,


Pieter


 
Reply With Quote
 
 
 
 
Rick
Guest
Posts: n/a
 
      16th Nov 2007
AFAIK a datagridview cannot bind to child properties.

Rick

"Pieter" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
> DataPropertyName in the DataGridViewTextBoxColumn, but this doesn't show
> the value...
>
> Does anybody knows how to do this?
>
>
> Thansk a lot in advance,
>
>
> Pieter
>



 
Reply With Quote
 
Duy Lam
Guest
Posts: n/a
 
      16th Nov 2007
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 DataPropertyName
> in the DataGridViewTextBoxColumn, 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="companyGridView"
OnItemDataBound="companyGridView_ItemDataBound">
<HeaderTemplate><table><tr><td>Company</td></tr></HeaderTemplate>
<FooterTemplate></table></FooterTemplate>
<ItemTemplate>
<tr><td><asp:Label Text="<%# Eval("CompanyName") %>">
<asp:Repeater Id="addressGridView">
<HeaderTemplate><table><tr><td>Address</td></tr></HeaderTemplate>
<FooterTemplate></table></FooterTemplate>
<ItemTemplate>
<tr><td><asp:Label Text="<%# Eval("CompanyAddress") %>">
</td></tr>
</ItemTemplate>
</asp:Repeater>
</td></tr>
</ItemTemplate>
</asp:Repeater>


In your code behind:

protected void companyGridView_ItemDataBound(object sender,
RepeaterItemEventArgs e) {
if( e.Item.ItemType == ItemType.Item || e.Item.ItemType ==
ItemType.AlternatingItem) {
Company boundObj = e.Item.DataItem as Company; // your Company
object which is binding
Repeater rpt = e.Item.FindControl("addressGridView") as Repeater; //
ger reference
rpt.DataSource = Companay.Address; // 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
 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      16th Nov 2007
The "DataGridViewTextBoxColumn" (and the "windowsforms" group in the
cross-post) indicates that this is windows-forms, not asp.net, but
this is an easy mistake to make when it isn't clear.

For the OP's benefit - if you limit yourself to a single appropriate
group (such as windowsforms) then this type of mixup is avoided. For
future questions, you might want to consider
microsoft.public.dotnet.framework.windowsforms.databinding.

DataGridView (winform) only supports direct properties for columns.
Adding a facade method could be an option if you own the class. If
not, there are some other (more complex) tricks you can use to provide
a runtime shadow property. Let me know if you need this, but note that
a facade method is preferable for simplicity, i.e.

public string EmployeeName { // facade to Employee.Name
get {return Employee.Name;}
set {Employee.Name = value;}
}

Marc


 
Reply With Quote
 
Duy Lam
Guest
Posts: n/a
 
      27th Nov 2007
Marc Gravell wrote:
> The "DataGridViewTextBoxColumn" (and the "windowsforms" group in the
> cross-post) indicates that this is windows-forms, not asp.net, but
> this is an easy mistake to make when it isn't clear.
>
> For the OP's benefit - if you limit yourself to a single appropriate
> group (such as windowsforms) then this type of mixup is avoided. For
> future questions, you might want to consider
> microsoft.public.dotnet.framework.windowsforms.databinding.
>
> DataGridView (winform) only supports direct properties for columns.
> Adding a facade method could be an option if you own the class. If
> not, there are some other (more complex) tricks you can use to provide
> a runtime shadow property. Let me know if you need this, but note that
> a facade method is preferable for simplicity, i.e.
>
> public string EmployeeName { // facade to Employee.Name
> get {return Employee.Name;}
> set {Employee.Name = value;}
> }
>
> Marc
>
>


Oh, I'm sorry

--
Duy Lam Phuong
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bind Property of Child-Objecty to DataGridView Pieter Microsoft VB .NET 4 27th Nov 2007 02:52 PM
Bind Property of Child-Objecty to DataGridView Pieter Microsoft ADO .NET 4 27th Nov 2007 02:52 PM
Bind Property of Child-Objecty to DataGridView Pieter Microsoft Dot NET Framework Forms 4 27th Nov 2007 02:52 PM
Bind Property of Child-Objecty to DataGridView Pieter Microsoft Dot NET 4 27th Nov 2007 02:52 PM
Can I bind DataGridView to xml? Stan Microsoft Dot NET Framework 1 1st Feb 2006 08:38 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:49 AM.