Add Columns to DataGrid from DataSet that has multiple table?

W

Wing

Hi all,

I am using Microsoft Visual Studio .NET 2003 to program my code. I have
following question.

I am trying to adding a few bound columns which from a dataset that
containing 2 tables to a datagrid, and display to the user. once I run
it, I have error msg:

"A field or property with the name 'PhotoName' was not found on the
selected datasource."

where PhotoName is the first Column in second table of the dataset.

my code is showing below
----------------------------------­ ---------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace relation_test
{
public class WebForm1 : System.Web.UI.Page
{
protected System.Data.SqlClient.SqlConnectio­ n thisConnection;
protected System.Web.UI.WebControls.DataGrid­ DataGrid1;

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
string found = "ae627ecb-a964-4672-a1f7-33261f450a6c";­

DataSet shoppingCartDataSet = new DataSet();

SqlDataAdapter shoppingCartAdapter = new SqlDataAdapter (
"SELECT * FROM ShoppingCart WHERE CartID='"+found+"'", thisConnection);

SqlDataAdapter photoAdapter = new SqlDataAdapter ("SELECT PhotoID,
PhotoName, Price FROM T_Photo WHERE PhotoID IN (SELECT PhotoID FROM
ShoppingCart WHERE CartID='"+found+"')", thisConnection);


shoppingCartAdapter.Fill(shoppingC­ artDataSet, "ShoppingCart");
photoAdapter.Fill(shoppingCartData­ Set, "T_Photo");

DataRelation thisRelation = shoppingCartDataSet.Relations.Add(­
"PhotoOrder",
shoppingCartDataSet.Tables[ShoppingCart].Columns[PhotoID],
shoppingCartDataSet.Tables[T_Photo].Columns[PhotoID]);

DataGrid1.DataSource = shoppingCartDataSet;
DataGrid1.DataBind();
}
}

}
}

I found that, the datagrid can locate the columns in frist table
"ShoppingCart" with no problem, but can not find the columns in second
table "T_Photo".

Can anyone tell me how to solve this problem, so I can display any
columns selectively from the dataset.

thanks for your time.
 
I

Ignacio Machin \( .NET/ C# MVP \)

hi,

you cannot do it, it has to be one table.
What you can do though, is create "link" columns, where a column in one
table (parent) is a reference to a column in a child table. Take a look at
DataColumn.Expression in MSDN


cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



Hi all,

I am using Microsoft Visual Studio .NET 2003 to program my code. I have
following question.

I am trying to adding a few bound columns which from a dataset that
containing 2 tables to a datagrid, and display to the user. once I run
it, I have error msg:

"A field or property with the name 'PhotoName' was not found on the
selected datasource."

where PhotoName is the first Column in second table of the dataset.

my code is showing below
----------------------------------­ ---------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace relation_test
{
public class WebForm1 : System.Web.UI.Page
{
protected System.Data.SqlClient.SqlConnectio­ n thisConnection;
protected System.Web.UI.WebControls.DataGrid­ DataGrid1;

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
string found = "ae627ecb-a964-4672-a1f7-33261f450a6c";­

DataSet shoppingCartDataSet = new DataSet();

SqlDataAdapter shoppingCartAdapter = new SqlDataAdapter (
"SELECT * FROM ShoppingCart WHERE CartID='"+found+"'", thisConnection);

SqlDataAdapter photoAdapter = new SqlDataAdapter ("SELECT PhotoID,
PhotoName, Price FROM T_Photo WHERE PhotoID IN (SELECT PhotoID FROM
ShoppingCart WHERE CartID='"+found+"')", thisConnection);


shoppingCartAdapter.Fill(shoppingC­ artDataSet, "ShoppingCart");
photoAdapter.Fill(shoppingCartData­ Set, "T_Photo");

DataRelation thisRelation = shoppingCartDataSet.Relations.Add(­
"PhotoOrder",
shoppingCartDataSet.Tables[ShoppingCart].Columns[PhotoID],
shoppingCartDataSet.Tables[T_Photo].Columns[PhotoID]);

DataGrid1.DataSource = shoppingCartDataSet;
DataGrid1.DataBind();
}
}

}
}

I found that, the datagrid can locate the columns in frist table
"ShoppingCart" with no problem, but can not find the columns in second
table "T_Photo".

Can anyone tell me how to solve this problem, so I can display any
columns selectively from the dataset.

thanks for your time.
 
W

Wing

thanks for your advice, I will take a look at DataColumn, hope that can
help
thanks a lot
 

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