Live Chat Icon For mobile
Live Chat Icon

How to group the data in the DataList

Platform: ASP.NET| Category: DataList

<asp:DataList id='DataList1' runat='server' onItemDataBound='ItemDB'> 
<HeaderTemplate > 
    <table width=100%> 
   <asp:Label Runat=server 
   text=<%#DataBinder.Eval(Container.DataITem, 'Title')%> ID='lblTitle'> 
   <td><asp:Label Runat=server 
   text=<%#DataBinder.Eval(Container.DataITem, 'LastName')%> ID='lblLastName'> 


Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
End If
End Sub

’Bind Data to DataList Populating the Dataset
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
End Sub

’The ItemDataBound Event
Protected Sub ItemDB(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs)
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
		title = strval
		ViewState('title') = title
		CType(e.Item.FindControl('lblTitle'), Label).Text = title
		e.Item.Visible = True
	End If
End If
End Sub


void Page_Load(object sender, EventArgs e) 
	if (!(Page.IsPostBack)) 

void BindTitle() 
	DataSet ds = new DataSet(); 
	string sqlStmt = 'SELECT * FROM Employees order by title'; 
	string conString = 'server=localhost;database=Northwind;uid=sa;pwd=;'; 
	SqlDataAdapter myda = new SqlDataAdapter(sqlStmt, conString); 
	myda.Fill(ds, 'Table'); 
	DataList1.DataSource = ds; 

protected void ItemDB(object sender, System.Web.UI.WebControls.DataListItemEventArgs e) 
	if (e.Item.ItemType == ListItemType.AlternatingItem | e.Item.ItemType == ListItemType.Item) 
		string strval = ((Label)(e.Item.FindControl('lblTitle'))).Text; 
		string title = ViewState('title'); 
		if (title == strval) 
			((Label)(e.Item.FindControl('lblTitle'))).Text = ''; 
			e.Item.Visible = false; 
			title = strval; 
			ViewState('title') = title; 
			((Label)(e.Item.FindControl('lblTitle'))).Text = title; 
			e.Item.Visible = true; 

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.