By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,458 Members | 1,250 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,458 IT Pros & Developers. It's quick & easy.

Linking to datagrids together

P: n/a
How do i make it so that each time i select a different row in my
datagradview i want the data in my other to change accordingly.For
example, i will have invoices in the one grid and purchased items in
the other and when i select a invoice i want to bring up all the item
linked to the invoice
Jun 27 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
You need to set exactly the same DataSource for each, and use DataMember
(on the second grid) to specify the property to use to obtain the
sub-data. This ensures that the same "currency manager" is used when
binding the outer items; here's a C# 3 example, but works the same in C#
2...

Marc

using System.Collections.Generic;
using System.Windows.Forms;
class Foo
{
public string Name { get; set; }
List<Barbars = new List<Bar>();
public List<BarBars { get { return bars; } }
}
class Bar
{
public string Desc { get; set; }
}
static class Program
{
static void Main()
{
List<Foolist = new List<Foo{
new Foo {
Name = "Fred",
Bars = {
new Bar {Desc="abc"},
new Bar {Desc="def"}
}
}, new Foo {
Name = "Barney",
Bars = {
new Bar {Desc="ghi"}
}
}
};
Application.EnableVisualStyles();
Application.Run
(
new Form
{
Width = 600,
Text = "Linked bindings",
Controls =
{
new DataGridView {
Dock = DockStyle.Fill,
DataSource = list,
DataMember = "Bars"
},
new DataGridView
{
Dock = DockStyle.Left,
DataSource = list
}

}
}
);
}
}
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.