Sample Code: Grouping Data in the DataList
ASP.NET
Thread Starter: Paschal
Started:
Replies: 0

Ireland .NET Developer Alliance :: Forums » Technologies » ASP.NET » Sample Code: Grouping Data in the DataList
« Previous Thread Next Thread »
   30 Oct 2004, 7:03
Paschal is not online. Last active: 21/01/2005 18:37:42 Paschal
Sample Code: Grouping Data in the DataList

One of the most common question is that if a Datalist displays the data as

                                    
                                      
Inside Sales Coordinator Callahan
Sales Manager Buchanan
Sales Representative Suyama
Sales Representative King
Sales Representative Davolio
Sales Representative Leverling
Sales Representative Peacock
Sales Representative Dodsworth
Vice President, Sales Fuller
                                    
                                      How to display the result in a way that :
                                    
                                  
                                    
                                      Sales Manager,Sales Representative...etc appear only once. 
                                    
                                  
                                      
                                        
Inside Sales Coordinator Callahan
Sales Manager Buchanan
Sales Representative Suyama
King
Davolio
Leverling
Peacock
Dodsworth
Vice President, Sales Fuller
                                      
                                        Here goes the code
                                      
                                    

HTML Source



   


  

  
  



  


                                      
                                        Code Behind
                                      
                                    
                                      
                                        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 order by title"
  Dim conString As String = "server=localhost;database=Northwind;uid=sa;pwd=;"
  Dim myda As SqlDataAdapter = New SqlDataAdapter(sqlStmt, conString)
  myda.Fill(ds, "Table")
  DataList1.DataSource = ds
  DataList1.DataBind()
End Sub
                                      
                                        Private Sub DataList1_ItemDataBound(ByVal sender As Object, _ 
                                        
ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemDataBound
If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
  Dim strval As String = CType(e.Item.FindControl("lblTitle"), Label).Text
  Dim title As String = ViewState("title")
  If title = strval Then
    CType(e.Item.FindControl("lblTitle"), Label).Text = ""
    e.Item.Visible = False
  Else
    title = strval
    ViewState("title") = title
    CType(e.Item.FindControl("lblTitle"), Label).Text = title
    e.Item.Visible = True
  End If
End If
End Sub

The two other similar kinds of code can be found at

Customizing the data
Display Alphabetically Sorted Data in a DataGrid


  
  Page 1 of 1 (1 items)
Ireland .NET Developer Alliance :: Forums » Technologies » ASP.NET » Sample Code: Grouping Data in the DataList

Powered by Community Server :: Forums