Live Chat Icon For mobile
Live Chat Icon

How to change the colors of a row to indicate its the Birthday of the Employee (for example) in a DataList

Platform: ASP.NET| Category: DataList

<asp:datalist id='DataList1' OnItemDataBound ='ItemDB' GridLines='Both' RepeatColumns='2' runat='server' ShowHeader='true'>
	<HeaderStyle Font-Bold='True' HorizontalAlign='Center' Font-Name='verdana' Font-Size='10pt'></HeaderStyle>
	<ItemStyle Font-Size='10pt'></ItemStyle>
	<Headertemplate>
		Employee Details
	</Headertemplate>
	<ItemTemplate>
		<%#DataBinder.Eval(Container.DataItem,'LastName')%>
		,
		<%#DataBinder.Eval(Container.DataItem,'FirstName')%>
		<br>
		HireDate :
		<%#DataBinder.Eval(Container.DataItem,'HireDate')%>
		<br>
	</ItemTemplate>
</asp:datalist>

VB.NET


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ’Put user code to initialize the page here
	’Populate the DataList with DataSet
End Sub

Protected Sub ItemDB(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemDataBound
	Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)
	Dim BDate As DateTime
	If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
		’To check if the BirthDate has null values
	            	If Not drv.Row('BirthDate').ToString = DBNull.Value.ToString Then
	                ’Assign the BirthDate to variable BDate
                		BDate = DateTime.Parse(drv.Row('BirthDate').ToString)
		End If

	            	’If the Employee BirthDay is Today 
	            	’Change the row Text Color to blue
            		If BDate.ToString('dd/MM') = DateTime.Now.ToString('dd/MM') Then
		                e.Item.ForeColor = Color.Blue
		                e.Item.Font.Bold = True
		End If
	End If
End Sub

C#


private void Page_Load(object sender, System.EventArgs e)
{
	// Put user code to initialize the page here
	DataList1.DataSource =BindDataClass.BindData ();
	DataList1.DataBind (); 
}

protected void ItemDB(Object sender   , System.Web.UI.WebControls.DataListItemEventArgs   e  )
{
	DataRowView drv    = (DataRowView)e.Item.DataItem;
	DateTime BDate=Convert.ToDateTime (null) ; 
	if ((e.Item.ItemType == ListItemType.Item )||( e.Item.ItemType == ListItemType.AlternatingItem))
	{																  //To check if the BirthDate has null values
		if(  drv.Row['BirthDate'].ToString() != DBNull.Value.ToString())
		{
			//Assign the BirthDate to variable BDate
			BDate = DateTime.Parse(drv.Row['BirthDate'].ToString());
		}
		//If the Employee BirthDay is Today 
		//Change the row Text Color to blue
		if (BDate.ToString('dd/MM') == DateTime.Now.ToString('dd/MM'))
		{
			e.Item.ForeColor = Color.Blue;
			e.Item.Font.Bold = true;
		}
	}
}

Share with

Related FAQs

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

Please submit your question and answer.