Live Chat Icon For mobile
Live Chat Icon

How to change the value of a field before it gets displayed in the datagrid

Platform: ASP.NET| Category: DataGrid

<asp:DataGrid id='DataGrid1' OnItemDataBound='ItemDB' runat='server'></asp:DataGrid>

VB.NET


Protected Sub ItemDB(ByVal s As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
	If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
		Select Case Trim(e.Item.Cells(3).Text)
	                Case 'Sales Representative'
                	    e.Item.Cells(3).Text = 'SR'
	                Case 'Vice President, Sales'
                	    e.Item.Cells(3).Text = 'VP'
	                Case 'Sales Manager'
                	    e.Item.Cells(3).Text = 'SM'
	                Case 'Inside Sales Coordinator'
                	    e.Item.Cells(3).Text = 'ISC'
	                Case Else
                	    e.Item.Cells(3).Text = e.Item.Cells(3).Text
      		End Select
	end if
end sub

C#


protected void ItemDB (Object s  , System.Web.UI.WebControls.DataGridItemEventArgs e  )
{
	if ((e.Item.ItemType ==ListItemType.Item) ||(e.Item.ItemType ==ListItemType.AlternatingItem))
	{
		switch   ( e.Item.Cells[3].Text.Trim() ) 
		{
			case 'Sales Representative':
				e.Item.Cells[3].Text = 'SR';
				break;
			case 'Vice President, Sales':
				e.Item.Cells[3].Text = 'VP';
				break;
			case 'Sales Manager':
				e.Item.Cells[3].Text = 'SM';
				break;
			case 'Inside Sales Coordinator':
				e.Item.Cells[3].Text = 'ISC';
				break;
			default :
				e.Item.Cells[3].Text = e.Item.Cells[3].Text;
				break;
		}
	}
}

Share with

Share on twitter
Share on facebook
Share on linkedin