Add Columns to DataGrid from DataSet that has multiple table?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Wing

    Add Columns to DataGrid from DataSet that has multiple table?

    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.Collecti ons;
    using System.Componen tModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.Sess ionState;
    using System.Web.UI;
    using System.Web.UI.W ebControls;
    using System.Web.UI.H tmlControls;
    using System.Data.Sql Client;

    namespace relation_test
    {
    public class WebForm1 : System.Web.UI.P age
    {
    protected System.Data.Sql Client.SqlConne ctio­ n thisConnection;
    protected System.Web.UI.W ebControls.Data Grid­ DataGrid1;

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

    DataSet shoppingCartDat aSet = new DataSet();

    SqlDataAdapter shoppingCartAda pter = 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) ;


    shoppingCartAda pter.Fill(shopp ingC­ artDataSet, "ShoppingCart") ;
    photoAdapter.Fi ll(shoppingCart Data­ Set, "T_Photo");

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

    DataGrid1.DataS ource = shoppingCartDat aSet;
    DataGrid1.DataB ind();
    }
    }

    }
    }

    I found that, the datagrid can locate the columns in frist table
    "ShoppingCa rt" 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.

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

    #2
    Re: Add Columns to DataGrid from DataSet that has multiple table?

    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.Expr ession in MSDN


    cheers,

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



    "Wing" <li.alwin@gmail .com> wrote in message
    news:1119878057 .845630.140590@ g49g2000cwa.goo glegroups.com.. .
    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.Collecti ons;
    using System.Componen tModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.Sess ionState;
    using System.Web.UI;
    using System.Web.UI.W ebControls;
    using System.Web.UI.H tmlControls;
    using System.Data.Sql Client;

    namespace relation_test
    {
    public class WebForm1 : System.Web.UI.P age
    {
    protected System.Data.Sql Client.SqlConne ctio­ n thisConnection;
    protected System.Web.UI.W ebControls.Data Grid­ DataGrid1;

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

    DataSet shoppingCartDat aSet = new DataSet();

    SqlDataAdapter shoppingCartAda pter = 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) ;


    shoppingCartAda pter.Fill(shopp ingC­ artDataSet, "ShoppingCart") ;
    photoAdapter.Fi ll(shoppingCart Data­ Set, "T_Photo");

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

    DataGrid1.DataS ource = shoppingCartDat aSet;
    DataGrid1.DataB ind();
    }
    }

    }
    }

    I found that, the datagrid can locate the columns in frist table
    "ShoppingCa rt" 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.


    Comment

    • Wing

      #3
      Re: Add Columns to DataGrid from DataSet that has multiple table?

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

      Comment

      Working...