473,586 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Rick

"Pieter" <pi************ ****@hotmail.co mwrote in message
news:uj******** ******@TK2MSFTN GP06.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
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

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 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
Nov 16 '07 #3
The "DataGridViewTe xtBoxColumn" (and the "windowsfor ms" 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.publi c.dotnet.framew ork.windowsform s.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
Nov 16 '07 #4
Marc Gravell wrote:
The "DataGridViewTe xtBoxColumn" (and the "windowsfor ms" 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.publi c.dotnet.framew ork.windowsform s.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
Nov 27 '07 #5

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

Similar topics

5
2904
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 been my experience for years and decades. Maybe I'm projecting hostility. I don't know. How can I ask about theory and concepts without offending...
2
2275
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 have created the dataset and defined the relationship between the two tables in the schema. I add a list box to a form and set it's datasource to...
3
1321
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 asp:hyperlink based on the content of the filed form. How do I do this? Code examples would help greatly. Thanks
2
8875
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 will load the data from that table in the DataSet. Assuming I've got two tables in the DataSet, how can I set the DataSource property of the...
12
2619
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 does this? Here is the code i'm using and i'm gettting an error: code: Try dbConn.GetCarInformation(CarID)
1
9243
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 DataBinding accepts as a data source either an IList or an IListSource at System.Windows.Forms.ListControl.set_DataSource(Object value)
2
3407
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) DataList1 is bound to an objectdatasource (ObjectDataSource1), and has a datakey (CategoryId). I want to bind DataList2 to another...
0
3249
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 time. If I type the code in the aspx manually it does get parsed correctly though. This is an example of the aspx code that gets parsed...
5
1392
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. Thanks for any help.
2
2058
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 true but when I create the child controls it has always reset to false. I know it has been set to true as I have trace written the value in the...
0
7915
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7841
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8204
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7965
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3838
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1184
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.