473,394 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Bind Property of Child-Objecty to DataGridView

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
Nov 16 '07 #1
4 2947
AFAIK a datagridview cannot bind to child properties.

Rick

"Pieter" <pi****************@hotmail.comwrote in message
news:uj**************@TK2MSFTNGP06.phx.gbl...
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

Nov 16 '07 #2
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
Nov 16 '07 #3
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.dat abinding.

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
Nov 16 '07 #4
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.dat abinding.

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
Nov 27 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Secret Guy | last post by:
Because of my experience posting various places over the last couple of weeks: I'm expecting to be greated with hostility for asking about concepts instead of "practical" things, since that has...
2
by: Jeff | last post by:
Hello All: What I am trying to do is bind a textbox (really several text boxes) based on a list box selection. Basically what I'm dealing with are two tables with a 1 to 1 relationship. I...
3
by: Hyrum Mortensen | last post by:
Hi, I want to have an asp:hyperlink (with target set to _blank) that points to a multi-field form a user would fill out. After submitting the form, I want to dynamically change the text of the...
2
by: Bennett Haselton | last post by:
I know how to create a DataAdapter that loads data from a data source into a table in a typed DataSet, and how to set the DataSource and DataMember properties of a DataGrid so that at run time it...
12
by: Mike | last post by:
I'm calling a component to get my data. The component is returning a dataset. I need to populate text boxes on my aspx page with the data within the datalist. Where can I find an example that...
1
by: john wright | last post by:
I have a dictionary oject I created and I want to bind a listbox to it. I am including the code for the dictionary object. Here is the error I am getting: "System.Exception: Complex...
2
by: Phil | last post by:
I have a datalist (DataList1)... with an itemtemplate... then a table - server side (Table1)... then another datalist (DataList2)... another itemtemplate... another table - server side (Table2)...
0
by: Jeremy Chapman | last post by:
I have included below virtually all the code to a control I'm trying to build. My issue is that an array list property in my control does not get persisted properly to the aspx page code in design...
5
by: Jaybuffet | last post by:
a custom object. Similar to how I bind a collection of objects to a repeater. Is there a way to bind a single object to a Table? I could use a repeater, but seems over kill for a single object....
2
by: Chris | last post by:
I have created a date picker user control. I want to have a boolean property, which I can set to determine whether the control defaults to today's date. I have created a property which I set to...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.