473,625 Members | 3,249 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.

Nov 17 '05 #1
2 2058
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******@gmail .com> wrote in message
news:11******** **************@ 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.
Nov 17 '05 #2
thanks for your advice, I will take a look at DataColumn, hope that can
help
thanks a lot

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
7679
by: Billy Jacobs | last post by:
I am using a datagrid to display some data. I need to create 2 header rows for this grid with columns of varying spans. In html it would be the following. <Table> <tr> <td colspan=8>Official Impact Summary</td> </tr> <tr> <td colspan=2></td>
13
4619
by: DraguVaso | last post by:
Hi, I need a function that gives me the number of Columns shown in a DataGrid. So I don't need to know the number of columns shown in tha DataSource, because this number can be completely something else than the number of columns defined in the currently active TableStyle! I currently use DataGrid.TableStyles(0).GridColumnStyles.Count, but i know this won't work when using more than one TableStyle, or having a TableStyle with another...
1
4088
by: Fresh Air Rider | last post by:
Hello Does anyone know how to display data from a data source such as an array or SqlDataReader or XML etc within an HTML table whose number of columns can change according to the value of a variable ? This is one thing that XSL is brilliant at but I can't find a nice ..Net solution The following is how it can easily be achieved in XSL.
3
2537
by: Diego TERCERO | last post by:
Hi... I'm working on a tool for editing text resources for a family of software product my company produces. These text resources are found in a SQL Server database, in a table called "Resource" with the following structure : Resource{,en,fr,es} Yes.. these are the only languages supported actually. A couple of rows in that table would look like this :
3
1759
by: Alexander | last post by:
Hi, I am using a DataGrid and assign a DataTable as a DataSource to it: void ShowData() { DataTable myNewDataTable = new DataTable(); ... // fill table this.m_DataGrid1.DataSource = myNewDataTable; this.Refresh(); }
2
6390
by: CSL | last post by:
I am using the DataGrid in a Windows Application, how can I adjust the widths of each column individually.
1
1241
by: jwogick | last post by:
I'm hoping someone can help me! I just have a simple form with a datagrid displaying child records from a dataset that contains two related tables Parent table>Clients Child table>cases. using the designer I tell the datagrid to display the child records only from the dataset (Relation called ClientsCases table>cases) it will display the data fine but when I add a new tablestyle with custom columns it does not change the columns in the...
4
1903
by: Jeff | last post by:
I am stuck on trying to generate two columns headers for a datagrid on form load. I can use a datatable as the datasource and get the results I want, but I want to set different column widths and don't see a way to do this through the datagrid properties. I am not using a database connection, I just want to set column headers on the datagrid. I am somewhat new to vb.net. Here is the code I am using. This code works when the...
3
1387
by: RN1 | last post by:
I am retrieving records from a DB table & displaying them in a DataGrid. There are 6 columns in the DB table & the DataGrid displays all the 6 columns. This is the code: <script runat="server"> Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs) Dim dSet As DataSet Dim sqlConn As SqlConnection Dim sqlDapter As SqlDataAdapter
0
8189
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8635
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8356
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8497
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7184
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4089
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4193
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2621
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.