Hi Bob,
Thanks for posting in the community!
From your description, you have two relationed DataTables, in one
table(Widget) it has a column which represent another table(Model)'s
primarykey. Then, you'd like to bind the two tables onto a webform datagrid
control. The DataGrid is binded to the Widget table and in one template
column it need to display the related data in the Model table via the
certain column's value in Widget table, yes?
If there is anything I misunderstood, please feel free to let me know.
As for this problem, I think you can use the DataGrid's "ItemDataBound"
Event to accomplish the work. The "ItemDataBound" event is fired when the
each DataGridRow has been bind with the proper data retrieved from the
datasource. For detailed info on the "ItemDataBound" event, you can view
the follwing reference in MSDN:
#DataGrid.ItemDataBound Event
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemwebuiwebcontro
lsdatagridclassitemdataboundtopic.asp?frame=true
So we can retrieve the certain model_id column in Widget Table from the
DataGridRow's binded dataitem, and then query out its detailed record in
the Model Table. For example:
--------------
private void dgMain_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
DataRowView drv = (DataRowView)e.Item.DataItem;
string model_id =
drv["model_id"].ToString();
......query the proper data from the Model table and bind to certain
controls
}
}
------------------------
To make it clearly, I've made a generic sample page to show this means,
here is the page's code:
------------------------------aspx page----------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>HierarchicalDataGrid</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="
http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="600" align="center">
<tr>
<td><asp:datagrid id="dgMain" runat="server"
AutoGenerateColumns="False" BorderWidth="1px" BorderStyle="Solid">
<Columns>
<asp:BoundColumn DataField="w_id" HeaderText="ID"></asp:BoundColumn>
<asp:BoundColumn DataField="w_name"
HeaderText="Name"></asp:BoundColumn>
<asp:BoundColumn DataField="w_description"
HeaderText="Description"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Related Model">
<ItemTemplate>
ModelID:
<asp:Label ID="lblModelID" Runat="server"></asp:Label>
<br>
ModelName:
<asp:Label ID="lblModelName" Runat="server"></asp:Label>
<br>
ModelDescription:
<asp:Label ID="lblModelDescription" Runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid></td>
</tr>
<tr>
<td></td>
</tr>
</table>
</form>
</body>
</HTML>
------------------code behind page class--------------------------
public class HierarchicalDataGrid : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgMain;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
LoadData();
BindGrid();
}
}
protected void LoadData()
{
DataTable tbWidget = new DataTable("Widget");
DataTable tbModel = new DataTable("Model");
DataColumn[] keys = new DataColumn[1];
tbWidget.Columns.Add("w_id");
tbWidget.Columns.Add("w_name");
tbWidget.Columns.Add("w_description");
tbWidget.Columns.Add("w_model");
tbModel.Columns.Add("m_id");
tbModel.Columns.Add("m_name");
tbModel.Columns.Add("m_description");
for(int i=1;i<=8;i++)
{
DataRow mrow = tbModel.NewRow();
mrow["m_id"] = "Model_" + i.ToString();
mrow["m_name"] = "ModelName_" + i.ToString();
mrow["m_description"] = "ModelDescription_" + i.ToString();
tbModel.Rows.Add(mrow);
}
for(int i=1;i<=20;i++)
{
DataRow wrow = tbWidget.NewRow();
wrow["w_id"] = i.ToString();
wrow["w_name"] = "WidgetName_" + i.ToString();
wrow["w_description"] = "WidgetDescription_" + i.ToString();
wrow["w_model"] = tbModel.Rows[i%tbModel.Rows.Count]["m_id"];
tbWidget.Rows.Add(wrow);
}
DataSet ds = new DataSet();
ds.Tables.Add(tbWidget);
ds.Tables.Add(tbModel);
Session["TEMP_DATA"] = ds;
}
protected void BindGrid()
{
DataSet ds = (DataSet)Session["TEMP_DATA"];
DataTable tb = ds.Tables["Widget"];
dgMain.DataSource = tb;
dgMain.DataBind();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.dgMain.ItemDataBound += new
System.Web.UI.WebControls.DataGridItemEventHandler(this.dgMain_ItemDataBound
);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void dgMain_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
DataRowView drv = (DataRowView)e.Item.DataItem;
DataSet ds = (DataSet)Session["TEMP_DATA"];
Label lblModelID = (Label)e.Item.Cells[3].FindControl("lblModelID");
Label lblModelName = (Label)e.Item.Cells[3].FindControl("lblModelName");
Label lblModelDescription =
(Label)e.Item.Cells[3].FindControl("lblModelDescription");
DataRow[] rows = ds.Tables["Model"].Select("m_id =
'"+drv["w_model"].ToString() +"'");
if(rows.Length>0)
{
lblModelID.Text = rows[0]["m_id"].ToString();
lblModelName.Text = rows[0]["m_name"].ToString();
lblModelDescription.Text = rows[0]["m_description"].ToString();
}
}
}
}
-----------------------------------------------
In addition, here is some further tech aritcles on webform DataGrid's
databinding:
#Hierarchical Data Binding in ASP.NET
http://msdn.microsoft.com/library/en-us/dnaspp/html/aspn-hierdatabinding.asp
?frame=true
#Creating Custom Columns for the ASP.NET Datagrid
http://msdn.microsoft.com/library/en-us/dnaspp/html/creatingcustomcolumns.as
p?frame=true
I believe they'll also be helpful to you.
Please try out the preceding suggestions. If you have any further
questions, please feel free to let me know.
Regards,
Steven Cheng
Microsoft Online Support
Get Secure!
www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)