.NET Forum / Languages / C# / March 2008
Data Bound Applications with ADO.NET and Custom Objects
|
|
Thread rating:  |
Vanessa - 17 Mar 2008 15:36 GMT Hi all!
I was reading this article about data binding:
http://msdn2.microsoft.com/en-us/magazine/cc163477.aspx
I have a question: if I add a new property (ContactName, for example) to Customer class and add a TextBox (readonly), how can I bind it? What I want is: when I change the Customer on the ComboBox (or select another item on the DataGridView), I’d like to update the TextBox ContactName.
Thank you,
Vanessa
Marc Gravell - 17 Mar 2008 16:05 GMT Well, you should be able to just use standard binding. It should appear in the list if you use the IDE. The trick is to use the same instance as the data-source for both the list (datagridview, etc) and the single item (textbox etc):
(note I haven't used notifications here for brevity)
using System; using System.Windows.Forms; using System.ComponentModel; class Foo { public string Name { get; set; } public DateTime DateOfBirth { get; set; } } static class Program { static void Main() { Application.EnableVisualStyles(); BindingList<Foo> list = new BindingList<Foo> { new Foo {Name="Fred", DateOfBirth=DateTime.Today}, new Foo {Name="Jo", DateOfBirth=DateTime.Today.AddDays(-20)} }; using(DataGridView dgv = new DataGridView { Dock = DockStyle.Fill, DataSource = list }) using(TextBox txt = new TextBox { Dock = DockStyle.Bottom, ReadOnly = true }) using (Form form = new Form { Controls = { dgv, txt } }) { txt.DataBindings.Add("Text", list, "Name"); //form.DataBindings.Add("Text", list, "Name"); Application.Run(form); } } }
Vanessa - 17 Mar 2008 17:56 GMT Hi Marc!
Thank you for your answer, but my question is about binding from another level (I think).
In the sample there is a datagridview where the datasource is from an order list. On the right side it is shown some fields in a form, like customer name, employee, and other fields from order list. The customer name is a combobox that is binding from a list of customers, but it is related to customer ID that is on the order list. What I want is to show another property from customer in a textbox, or in other words, just a textbox to show more information about the customer.
Thank you, Vanessa
> Well, you should be able to just use standard binding. It should appear in > the list if you use the IDE. The trick is to use the same instance as the [quoted text clipped - 39 lines] > } > } Marc Gravell - 18 Mar 2008 10:08 GMT Do you mean like this?
using System; using System.Windows.Forms; using System.ComponentModel; class Order { public int OrderId { get; set; } public string YourRef { get; set; } public string OurRef { get; set; } public Customer Customer { get; set; } public override string ToString() { return OurRef; }
} class Customer { public string Name { get; set; } public string Address { get; set; } public int CustomerId { get; set; } public override string ToString() { return Name; } } static class Program { static void Main() { Application.EnableVisualStyles();
var customers = new BindingList<Customer> { new Customer {CustomerId = 1, Name = "Fred", Address = "Somewhere"}, new Customer {CustomerId = 2, Name = "Barney", Address = "Somewhere else"}, }; var orders = new BindingList<Order> { new Order {OrderId = 1, OurRef = "001", YourRef = "abc", Customer = customers[0]}, new Order {OrderId = 2, OurRef = "002", YourRef = "def", Customer = customers[1]}, new Order {OrderId = 3, OurRef = "003", YourRef = "ghi", Customer = customers[0]}, new Order {OrderId = 4, OurRef = "004", YourRef = "jkl", Customer = customers[1]} };
using (Form form = new Form { Controls = { new DataGridView { Dock = DockStyle.Fill, DataSource = orders }, new TextBox { Dock = DockStyle.Bottom, ReadOnly = true, DataBindings = { {"Text",customers,"Address"} } }, new ComboBox { Dock = DockStyle.Bottom, DropDownStyle = ComboBoxStyle.DropDownList, DataSource = customers, DisplayMember = "Name", DataBindings = { {"SelectedItem", orders, "Customer"} } } } }) { Application.Run(form); } } }
Marc Gravell - 18 Mar 2008 10:29 GMT Note - if the order has a CustomerId, not a Customer (i.e. you want to match by matching a key, not by object reference) - then:
new ComboBox { Dock = DockStyle.Bottom, DropDownStyle = ComboBoxStyle.DropDownList, DataSource = customers, DisplayMember = "Name", ValueMember = "CustomerId", DataBindings = { {"SelectedValue", orders, "CustomerId"} } }
You don't need to change the textbox, since that is simply bound to the list of customers, with the current row selected automatically via sharing a currency-manager with the combobox.
Marc
Vanessa - 18 Mar 2008 12:45 GMT Thank you Marc! That is exactly what I need! Vanessa
> Note - if the order has a CustomerId, not a Customer (i.e. you want to match > by matching a key, not by object reference) - then: [quoted text clipped - 15 lines] > > Marc Vanessa - 18 Mar 2008 20:13 GMT Hi Marc!
Your example worked like I wanted, but now I need to go ahead.
Imagine that I have a list of cities and every customer has a city ID. It is like this:
class Customer { public string Name { get; set; } public string Address { get; set; } public int CityId { get; set; } ======> I added this property public int CustomerId { get; set; } public override string ToString() { return Name; } }
class City { public string Name { get; set; } public string State { get; set; } public int CityId { get; set; } public override string ToString() { return Name; } }
So, in your example, I need to add info about the city of the customer. It could be a combobox with the city name and a textbox with the state. How can I bind this level?
Thank you, Vanessa
> Note - if the order has a CustomerId, not a Customer (i.e. you want to match > by matching a key, not by object reference) - then: [quoted text clipped - 15 lines] > > Marc Vanessa - 18 Mar 2008 20:23 GMT Hi Marc!
Please forget the last question, I already did it and it is working well.
I just have a doubt: how can I clear the combos and textboxes when I click on the last row? Or, if I apply a filter on the datagridview and there will be no rows on it?
Thank you! Vanessa
> Note - if the order has a CustomerId, not a Customer (i.e. you want to match > by matching a key, not by object reference) - then: [quoted text clipped - 15 lines] > > Marc Claire - 17 Mar 2008 16:15 GMT You'd bind the textbox to the same field as the field in the datagridview. The textbox will update to reflect changes once the record is posted
class clsServerInfo { public string Address { get { return _address; } set { _address = value; } } }// end class
clsServerInfo ServerInfo = new clsServerInfo();
// bind the "Text" property of textbox txtAddress to // the contents of ServerInfo.Address txtAddress.DataBindings.Add("Text", ServerInfo, "Address");
Vanessa - 17 Mar 2008 17:56 GMT Hi Claire!
Thank you for your answer, but my question is about binding from another level (I think).
In the sample there is a datagridview where the datasource is from an order list. On the right side it is shown some fields in a form, like customer name, employee, and other fields from order list. The customer name is a combobox that is binding from a list of customers, but it is related to customer ID that is on the order list. What I want is to show another property from customer in a textbox, or in other words, just a textbox to show more information about the customer.
Thank you, Vanessa
> You'd bind the textbox to the same field as the field in the datagridview. > The textbox will update to reflect changes once the record is posted [quoted text clipped - 19 lines] > // the contents of ServerInfo.Address > txtAddress.DataBindings.Add("Text", ServerInfo, "Address");
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|