Tie one grid to another (master/detail)

G

Gina_Marano

Hey All,

Working with the .Net data has been the hardest part of my
transition. It just doesn't seem very intuitive.

I want to have a master/detail relationship between 2 grids. When the
user navigates grid 1, only the details for that record appear in grid
2. How?

I created a datasource and added the 2 tables. The relationship
between the two tables was created. Now what? How do I get the first
grid to act like a filter for the second grid?

I am using devexpress grids so hopefully this isn't grid specific.

TIA

~GINA_M~
 
A

Ashot Geodakov

I usually use DataGridView in very rare cases when all that's needed is to
display some single table. Anything more complicated than that, and this
control falls out of the question.

For your task I'd use a pare of regular ListView controls, which I'd
populate programmatically.

SqlConnection connection = new SqlConnection( connectionString );
connection.Open();

SqlCommand cmd1 = new SqlCommand( "select * from table1", connection );
SqlDataReader reader = cmd1.ExecuteReader();

while( reader.Read() )
{
// add rows to ListView1:
ListViewItem item = listView1.Items.Add( (int)reader[0] ); // Say, some
record ID
item.Subitems.Add( (string)reader[1] ); // record description
}

reader.Close();

// Now handle the selection change in ListView1 and populate ListView2:
void listView1_SelectionChanged( ... ) // or whatever the event's name
{
SqlCommand cmd2 = new SqlCommand( "select * from table2 where ID = " +
listView1.SelectedItems[0].Text, connection );
reader = cmd2.ExecuteReader();

while( reader.Read() )
{
// add rows to ListView2:
ListViewItem item = listView2.Items.Add( (int)reader[0] ); // some
record ID
item.Subitems.Add( (string)reader[1] ); // record description
}
reader.Close();
}

// On application exit:
connection.Close();
 
M

Michael Nemtsev

Hello Gina_Marano,

See there http://www.syncfusion.com/FAQ/winforms/FAQ_c44c.aspx#q780q

---
WBR, Michael Nemtsev [.NET/C# MVP].
My blog: http://spaces.live.com/laflour
Team blog: http://devkids.blogspot.com/

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo

G> Hey All,
G>
G> Working with the .Net data has been the hardest part of my
G> transition. It just doesn't seem very intuitive.
G>
G> I want to have a master/detail relationship between 2 grids. When the
G> user navigates grid 1, only the details for that record appear in
G> grid 2. How?
G>
G> I created a datasource and added the 2 tables. The relationship
G> between the two tables was created. Now what? How do I get the first
G> grid to act like a filter for the second grid?
G>
G> I am using devexpress grids so hopefully this isn't grid specific.
G>
G> TIA
G>
G> ~GINA_M~
G>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top