Template controls and hierarchical data

B

bclegg

Hi,
I have relational data contained in a dataset, say a widget table and a
model table.
Each widget has a model_id which relates to the primary key of the Model
table.
In the model table is a description field.

The datagrid is bound to Widget table.
The appropriate templated columns for widget information contain labels
bound to Container.dataItem fields

In the model column I have bound the label straight to the
dataset.table.field ie myDataset.Model.description.

Problem is that it just displays the description from the first record
in the model table.
ie. It is not finding the correct record from the model table via the
relation
with the widget table.

Is it possible to do this or do I have to build a flat view of the data?
Thanks
Bob
 
S

Steven Cheng[MSFT]

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.)
 
S

Steven Cheng[MSFT]

Hi Bob,


Have you had a chance to try out my suggestions or have got any progresses
on this issue? If you have any questoins or need any further help, 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.)
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top