Live Chat Icon For mobile
Live Chat Icon

How to add a counter column to a DataGrid

Platform: ASP.NET| Category: DataGrid

<asp:Datagrid id='Datagrid1' runat='server'>
<Columns> 
<asp:TemplateColumn> 
<ItemTemplate> 
<asp:Label id='Label1' Runat ='server' > <%#getCount%> </asp:Label> 
</ItemTemplate> 
</asp:TemplateColumn>
</Columns>
</asp:Datagrid>

Code behind
VB.NET


Dim count As Integer
protected function getCount() As Integer
	count = count + 1
	Return count
End Function

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
’Populate the Datagrid populating data from the database
’..... 
End Sub 

C#


int  count =0;
protected int getCount()
{
	count = count + 1;
	return count;
}

private void Page_Load(object sender, System.EventArgs e)
{
//Populate the Datagrid populating data from the database
//....
}

Share with

Share on twitter
Share on facebook
Share on linkedin