Hello Bill,
Welcome to the ASPNET newsgroup.
From your description, you're building a data display page in ASP.NET
application(2.0 ). Currently you have
a Dataset that contains multiple datatables which have relationship with
eachother, however, when bind a certain datatable in the dataset to a
Gridview, you found the gridview only display one dimension data only (with
out the related parent table's values) and you're wondering how to make the
GridView display the related table's data(through the foreign key column),
correct? If there is anything I missed or didn't quite address, please feel
free to let me know.
Based on my understanding, the behavior yet is expected due to the ASP.NET
webform page's databinding mechanism. ASP.NET template control databinding
is quite different from winform control databinding. In winform, since the
datasoure is always in memory, the databound context is easy to track the
datasource and navigate between the current bound datatable to its related
parent or child table. However, in ASP.NET since the page must be flushed
and write out to clent, it can not hold the datasource in memory forever,
it just query the current attached datasource(tabl e or view) and bind that
direct attached table/view's data/columns to the databound
control(Gridvie w, datagrid ....). that's why you'll find GridView directly
display the foreign key value rather than the detailed value in
parent/child table/view.
To resolve this in webform page, we need to manually customize the
databinding process. for example, we can use the databound control such as
GridView's Item/Row DataBound event to do the customization.
#GridView.RowDa taBound Event
http://msdn2.microsoft.com/en-us/lib...trols.gridview.
rowdatabound.as px
In the RowDataBound event, we can access the current GridView Row's inner
sub controls(in each gridviewRow column). Also, we can access the current
databound data item(such as DataRowview). Then, we can manualy get the
related data (parent or sub rows through the DAtaRowView) and use it to
modify the certain Gridview row column's controls. for example:
the following Gridview is bound to a DataView(from a datatable which
contains the Northwind "products" table's data), and the container dataset
also contains another datatable ("categories " table in northwind), I add a
relation between them and bind the products view to the GridView. In the
RowDataBound event, I manually query parent row data from Categories table:
=====aspx template======= ===
<asp:GridView ID="GridView1" runat="server" AutoGenerateCol umns="False"
OnRowDataBound= "GridView1_RowD ataBound">
<Columns>
<asp:BoundFie ld DataField="Prod uctID"
HeaderText="Pro ductID" />
<asp:BoundFie ld DataField="Prod uctName"
HeaderText="Pro ductName" />
<asp:TemplateFi eld HeaderText="Cat egory">
<EditItemTempla te>
<asp:TextBox ID="txtCategory " runat="server"
Text='<%# Bind("CategoryI D") %>'></asp:TextBox>
</EditItemTemplat e>
<ItemTemplate >
<asp:Label ID="lblCategory " runat="server"
Text='<%# Bind("CategoryI D") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateFie ld>
</Columns>
</asp:GridView>
==============c ode behind========= =====
protected void Page_Load(objec t sender, EventArgs e)
{
if (!IsPostBack)
{
PerformDataBind ();
}
}
protected void PerformDataBind ()
{
//get the dataset which contains two datatables that have relation
DataSet ds = GetDataSet();
ds.Relations.Ad d("products_cat egories",
ds.Tables[1].Columns["CategoryID "], ds.Tables[0].Columns["CategoryID "]);
GridView1.DataS ource = ds.Tables[0].DefaultView;
GridView1.DataB ind();
}
protected void GridView1_RowDa taBound(object sender, GridViewRowEven tArgs e)
{
if (e.Row.RowType == DataControlRowT ype.DataRow)
{
DataRowView product = e.Row.DataItem as DataRowView;
string strCategory =
product.Row.Get ParentRow("prod ucts_categories ")["CategoryNa me"] as string;
Label lbl = e.Row.FindContr ol("lblCategory ") as Label;
lbl.Text = strCategory;
}
}
=============== ==========
Hope this helps. If you have anything unclear on this, please feel free to
post here.
Regards,
Steven Cheng
Microsoft Online Community Support
=============== =============== =============== =====
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.
Get Secure!
www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)