SIGN UP MEMBER LOGIN:    
ARTICLE

Customizing the Display of Data

Posted by Sushila Patel Articles | WebForms Controls July 02, 2003
This article shows you how to customize data display in a DataList control.
Reader Level:

The Data in the Table is as follows: 

 

Question:  

How to display the Data as given below:

Solution:

To get the functionality of displaying the data in a repetitive manner we will need a Datalist. The RepeatColumns Property of the DataList allows us to specify the number of columns to be repeated.

The column Title has duplicate Records like "Sales Representative", "Sales Manager".....To avoid the Title Columns to be repeated one way to to manipulate the data is using the SortedList (a class in the System.Collections namespace). This class is a collection of Key and Value pair. So we can check for if the value Exists and accordingly discard the value for the duplicate data.

Step 1:

So in webform1.aspx we'll have:

<asp:datalist id="DataList1" runat="server" ItemStyle-VerticalAlign="Top" RepeatColumns="4" Width="100%"><ItemTemplate>
<
asp:Label ID="lbl1" Runat="server" Font-Bold="true" Font-Size="10pt">
<%# Container.DataItem%></asp:Label>
<
asp:label id="lblETitle" Visible="False" runat="Server"
Text='<%#Container.DataItem%>' />
<
hr>
....
</ItemTemplate>
</
asp:datalist>

In webform1.asp.vb

To display the Title Column as Header. We write as follows

Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
BindTitle()
End If
End
Sub
Sub
BindTitle()
Dim ds As New DataSet
Dim sqlStmt As String = "SELECT * FROM Employees"
Dim conString As String = "server=localhost;database=Northwind;uid=sa;pwd=;"
Dim myda As SqlDataAdapter = New SqlDataAdapter(sqlStmt, conString)
myda.Fill(ds, "Table")
Dim slist As SortedList = New SortedList
Dim i As Integer
Dim dr As DataRow
For Each dr In ds.Tables(0).Rows
If Not slist.ContainsValue(dr("Title")) Then
slist.Add(dr("Employeeid"), dr("Title"))
End If
Next
DataList1.DataSource = slist.GetValueList
DataList1.DataBind()
End Sub

Step 2:

To display all the Employees for the Specific Title add a DataGrid within the DataList.

<asp:datalist id="DataList1" runat="server" ItemStyle-VerticalAlign="Top" RepeatColumns="4" Width="100%">
<
ItemTemplate>
...
<asp:DataGrid runat="server" id="Datagrid1" AutoGenerateColumns="False"
Font-Name="Verdana" Font-Size="10pt" ShowHeader="false"
ItemStyle-Font-Size="10pt" ItemStyle-Font-Name="verdana" BorderWidth="0" CellPadding="0" CellSpacing="0">
<
Columns>
<
asp:TemplateColumn HeaderText="Name">
<
ItemTemplate>
<%#DataBinder.Eval(Container.DataItem , "LastName")%>,<%#DataBinder.Eval(Container.DataItem , "FirstName")%>
</ItemTemplate>
</
asp:TemplateColumn>
</
Columns>
</
asp:DataGrid>
...
</ItemTemplate>
</
asp:datalist>

In code behind

Function getEmp(ByVal title As String) As DataSet
Dim ds As New DataSet
Dim sqlStmt As String = "SELECT * FROM Employees where title=" & "'" & title & "'"
Dim conString As String = "server=localhost;database=Northwind;uid=sa;pwd=;"
Dim myda As SqlDataAdapter = New SqlDataAdapter(sqlStmt, conString)
myda.Fill(ds, "Table")
Return ds
End Function

Private Sub DataList1_ItemDataBound(ByVal sender As Object, _ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemDataBound
Dim Titlelabel As Label = e.Item.FindControl("lblETitle")
Dim strEmpTitle As String = Titlelabel.Text
CType(e.Item.FindControl("Datagrid1"), DataGrid).DataSource = getEmp
strEmpTitle)
CType(e.Item.FindControl("Datagrid1"), DataGrid).DataBind()
End Sub

That all needed to run the code!!!!

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Gauge for SharePoint
Become a Sponsor