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

Show relational data in a second datagrid

P: n/a
Hi,

I have 2 talbes in a DataSet, and made a DataRelation between the 2
tables in the DataSet.

One of the Tables are displayed in a datagrid, and when one of them is
selected in the datagrid I want its childcolumns to be displayed in a
second datagrid.

How can I filter the dataset or get only the related data into the
second datagrid?

I totally blank on this one.

I use ByteFX to connect to a mysql database.

Here are my code:
private void Form1_Load(object sender, System.EventArgs e)
{
MySqlConnection thisConnection = new MySqlConnection("Server=syska.dk;
Username=root; Password=supermand; Database=c-sharp;");

MySqlDataAdapter thisPollAdapter = new MySqlDataAdapter("SELECT * FROM
poll", thisConnection);

MySqlDataAdapter thisOptionAdapter = new MySqlDataAdapter("SELECT *
FROM pollOption", thisConnection);

thisPollAdapter.Fill(thisDataSet, "first");

thisOptionAdapter.Fill(thisDataSet, "second");

dataGrid.DataSource = thisDataSet.Tables["first"];

DataRelation thisRelation = new DataRelation("thisRelation",
thisDataSet.Tables[0].Columns["pollId"],
thisDataSet.Tables[1].Columns["pollId"]);

// MessageBox.Show( thisDataSet.Tables[1].ParentRelations. );

// her skal de columns smides ind som passer til det ID som er valgt i
det anden datagrid
// dataGrid1.DataSource =
}

As you can see, I havent made anything fancy yet, are this the right way
to go, or are there are better way of doing this, I dont want to get all
the data every time, I choose a new row in the first datagrid..

kind regards
Mikael Syska
Nov 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Mikael,

It's relatively easy. For the data source on the child grid, set it to
the DataRelation object that relates the two grids.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mikael Syska" <ne**********@syska.dk> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi,

I have 2 talbes in a DataSet, and made a DataRelation between the 2 tables
in the DataSet.

One of the Tables are displayed in a datagrid, and when one of them is
selected in the datagrid I want its childcolumns to be displayed in a
second datagrid.

How can I filter the dataset or get only the related data into the second
datagrid?

I totally blank on this one.

I use ByteFX to connect to a mysql database.

Here are my code:
private void Form1_Load(object sender, System.EventArgs e)
{
MySqlConnection thisConnection = new MySqlConnection("Server=syska.dk;
Username=root; Password=supermand; Database=c-sharp;");

MySqlDataAdapter thisPollAdapter = new MySqlDataAdapter("SELECT * FROM
poll", thisConnection);

MySqlDataAdapter thisOptionAdapter = new MySqlDataAdapter("SELECT * FROM
pollOption", thisConnection);

thisPollAdapter.Fill(thisDataSet, "first");

thisOptionAdapter.Fill(thisDataSet, "second");

dataGrid.DataSource = thisDataSet.Tables["first"];

DataRelation thisRelation = new DataRelation("thisRelation",
thisDataSet.Tables[0].Columns["pollId"],
thisDataSet.Tables[1].Columns["pollId"]);

// MessageBox.Show( thisDataSet.Tables[1].ParentRelations. );

// her skal de columns smides ind som passer til det ID som er valgt i det
anden datagrid
// dataGrid1.DataSource =
}

As you can see, I havent made anything fancy yet, are this the right way
to go, or are there are better way of doing this, I dont want to get all
the data every time, I choose a new row in the first datagrid..

kind regards
Mikael Syska

Nov 16 '05 #2

P: n/a
Hey,

Fairly new to C#,

Can you give me a example

Can't seem to get the idea of how it knows witch item is selected in the
first datagrid, but you can probebly give me a breef description of it

kind regards
Mikael Syska

Nicholas Paldino [.NET/C# MVP] wrote:
Mikael,

It's relatively easy. For the data source on the child grid, set it to
the DataRelation object that relates the two grids.

Hope this helps.

Nov 16 '05 #3

P: n/a
Mikael,

You would want to do this:

// The grid is named childDataGrid, the DataSet is named dataSet.
childDataGrid.DataSource = dataSet.Relations[0];

You can substitute the relation for the proper one. The data set knows
it is a relation and will select the appropriate rows.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mikael Syska" <ne**********@syska.dk> wrote in message
news:ui**************@TK2MSFTNGP12.phx.gbl...
Hey,

Fairly new to C#,

Can you give me a example

Can't seem to get the idea of how it knows witch item is selected in the
first datagrid, but you can probebly give me a breef description of it

kind regards
Mikael Syska

Nicholas Paldino [.NET/C# MVP] wrote:
Mikael,

It's relatively easy. For the data source on the child grid, set it
to the DataRelation object that relates the two grids.

Hope this helps.


Nov 16 '05 #4

P: n/a
Nicholas,

I have just tried what you said...

now the code look like this:
MySqlConnection thisConnection = new MySqlConnection("Server=syska.dk;
Username=root; Password=supermand; Database=c-sharp;");

MySqlDataAdapter thisPollAdapter = new MySqlDataAdapter("SELECT *
FROM poll", thisConnection);
MySqlDataAdapter thisOptionAdapter = new MySqlDataAdapter("SELECT * FROM
pollOption", thisConnection);

thisPollAdapter.Fill(thisDataSet, "first");
thisOptionAdapter.Fill(thisDataSet, "second");

dataGrid.DataSource = thisDataSet.Tables["first"];

DataRelation thisRelation = new DataRelation("thisRelation",
thisDataSet.Tables[0].Columns["pollId"],
thisDataSet.Tables[1].Columns["pollId"]);

// My other datagrid1, this seems to work but no data is displayed
dataGrid1.DataSource = thisDataSet.Relations["thisRelation"];

// This gives me a error.... System.IndexOutOfRangeException: Cannot
find relation 0
// dataGrid1.DataSource = thisDataSet.Relations[0];

Can u see any errors or other mistyped things, cause it aint working....

Its so damm annoying that it aint working :-(
Nicholas Paldino [.NET/C# MVP] wrote:
Mikael,

You would want to do this:

// The grid is named childDataGrid, the DataSet is named dataSet.
childDataGrid.DataSource = dataSet.Relations[0];

You can substitute the relation for the proper one. The data set knows
it is a relation and will select the appropriate rows.

Nov 16 '05 #5

P: n/a
Tried a little more, and came a little closer, added this line to the
code and the Relation comes in the first DataGrid, but I would prefer
the other option with 2 datagrids.....

thisDataSet.Relations.Add(thisRelation);

Hope u have a solution for me

thanks on advance
Mikael Syska

Mikael Syska wrote:
Nicholas,

I have just tried what you said...

now the code look like this:
MySqlConnection thisConnection = new MySqlConnection("Server=syska.dk;
Username=root; Password=supermand; Database=c-sharp;");

MySqlDataAdapter thisPollAdapter = new
MySqlDataAdapter("SELECT * FROM poll", thisConnection);
MySqlDataAdapter thisOptionAdapter = new MySqlDataAdapter("SELECT * FROM
pollOption", thisConnection);

thisPollAdapter.Fill(thisDataSet, "first");
thisOptionAdapter.Fill(thisDataSet, "second");

dataGrid.DataSource = thisDataSet.Tables["first"];

DataRelation thisRelation = new DataRelation("thisRelation",
thisDataSet.Tables[0].Columns["pollId"],
thisDataSet.Tables[1].Columns["pollId"]);

// My other datagrid1, this seems to work but no data is displayed
dataGrid1.DataSource = thisDataSet.Relations["thisRelation"];

// This gives me a error.... System.IndexOutOfRangeException: Cannot
find relation 0
// dataGrid1.DataSource = thisDataSet.Relations[0];

Can u see any errors or other mistyped things, cause it aint working....

Its so damm annoying that it aint working :-(
Nicholas Paldino [.NET/C# MVP] wrote:
Mikael,

You would want to do this:

// The grid is named childDataGrid, the DataSet is named dataSet.
childDataGrid.DataSource = dataSet.Relations[0];

You can substitute the relation for the proper one. The data set
knows it is a relation and will select the appropriate rows.

Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.