How to display the total of a particular column at the footer of the DataGrid

Platform: ASP.NET| Category: DataGrid

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

VB.NET


Dim UnitPrice As Double
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
        	’Bind the Data to datagrid
End Sub

Protected Sub UPTotal(ByVal _unitprice As Double)
       	UnitPrice += _unitprice
End Sub ’UPTotal

Public Sub ItemDB(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
        	If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            		UPTotal(Double.Parse(e.Item.Cells(1).Text.ToString))
        	Else
            		If e.Item.ItemType = ListItemType.Footer Then
                		e.Item.Cells(0).Text = '  Total '
                		e.Item.Cells(1).Text = UnitPrice.ToString()
            		End If
        	End If
End Sub ’ItemDB 

C#


double UnitPrice;
private void Page_Load(object sender, System.EventArgs e)
{
	// Put user code to initialize the page here
	if(!Page.IsPostBack )
	{
		//Bind the DataGrid
	}
}
	
protected void UPTotal(double _unitprice)
{  
	UnitPrice  += _unitprice;
}

public void ItemDB(object sender, DataGridItemEventArgs e)
{
	if ((e.Item.ItemType == ListItemType.Item) ||( e.Item.ItemType == ListItemType.AlternatingItem))
	{
		UPTotal(Double.Parse ( e.Item.Cells[1].Text ));
	}
	else if (e.Item.ItemType ==ListItemType.Footer )
	{
		e.Item.Cells [0].Text ='  Total ';
		e.Item.Cells[1].Text =UnitPrice.ToString ();
	}	 
}

Share with

Related FAQs

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

Please submit your question and answer.