Display total in datagrid.

Display total in datagrid.  

reference codes:    (bellow are wrote by  Doug Seven  and I just put the main part on this bolg.  )

 ( By Doug Seven      
Published: 8/19/2002
Reader Level: Beginner
Rated: 3.33 by 3 member(s). )

The MyGrid_ItemDataBound event is called as each row in the data source is bound to the DataGrid. In this event handler you can work with the data in each row, in the form of a DataGridItem . For you purpose here, you will need to call CalcTotals and pass in the text from the Price column, and then format the Price column as currency for each row (Item or AlternatingItem ), and display the value of runningTotal in the Footer row.

public void MyDataGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
{
  if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
  {
    CalcTotal( e.Item.Cells[1].Text );
    e.Item.Cells[1].Text = string.Format("{0:c}" , Convert.ToDouble (e.Item.Cells[1].Text));
  }
  else if (e.Item.ItemType == ListItemType.Footer )
  {
    e.Item.Cells[0].Text="Total" ;
    e.Item.Cells[1].Text = string.Format("{0:c}" , runningTotal);
  }
}

from other documents: (for VB)


        
        
PROTECTED  As  sender myDataGrid_ItemDataBound(ByVal  Sub  System.Object,
ByVal e 
As  DataGridItemEventArgs) Handles myDataGrid.ItemDataBound        
Select   Case  e.Item.ItemType
            
Case  ListItemType.Item, ListItemType.AlternatingItem
                interimTotal 
+=  CType(e.Item.Cells( 3 ).Text,  Double )
            
Case  ListItemType.Footer
                e.Item.Cells(
2 ).Text  =   " TOTAL:  "
                e.Item.Cells(
3 ).Text  =  interimTotal.ToString
        
End   Select
End Sub

 part of my code:

my datagrid:

< asp:DataGrid  id ="dgReport"  runat ="server"  Width ="936px"  ShowFooter ="True"  AllowSorting ="True"
        AutoGenerateColumns
="False"  AllowPaging ="True" >
        
< FooterStyle  Font-Bold ="True" ></ FooterStyle >
        
< Columns >
            
< asp:TemplateColumn  SortExpression ="NET_AMT"  HeaderText ="Net Amt" >
                
< ItemTemplate >
                    
< asp:Label  id ="lblDgReportNetAmt"  runat ="server"  Text ='<%#  DataBinder.Eval(Container.DataItem, "NET_AMT") % > '>
                    
</ asp:Label >
                
</ ItemTemplate >
            
</ asp:TemplateColumn >
        
</ Columns >
        
< PagerStyle  HorizontalAlign ="Right" ></ PagerStyle >
    
</ asp:DataGrid >

part of C# code :

private   void  dgReport_ItemDataBound( object  sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
               
if  (e.Item.ItemType  ==  ListItemType.Item  ||  e.Item.ItemType  ==  ListItemType.AlternatingItem)
              
{
    Label lblNetAmt 
=  (Label) e.Item.Cells[ 5 ].FindControl( " lblDgReportNetAmt " );
    sumNetAmt 
+=   decimal .Parse(lblNetAmt.Text);
               }

               
else   if (e.Item.ItemType  ==  ListItemType.Footer )
              
{
    totalNetAmt 
=  sumNetAmt  +  totalNetAmt;

    e.Item.Cells[
0 ].Text = " Current Total " ;
    e.Item.Cells[
5 ].Text  =  totalNetAmt.ToString();
               }
 
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF DataGrid是一种用于显示数据的控件,而DataGrid.Columns则是DataGrid中的列集合。你可以通过设置DataGrid.Columns的Background属性来改变整个列的背景颜色。 在WPF中,你可以使用多种方式来设置DataGrid.Columns的Background属性,包括使用静态资源、绑定属性、使用样式等。以下是一些示例代码: 1. 使用静态资源设置DataGrid.Columns的Background属性: ``` <DataGrid> <DataGrid.Columns> <DataGridTextColumn Header="Column 1" Binding="{Binding Column1}"> <DataGridTextColumn.HeaderStyle> <Style TargetType="DataGridColumnHeader"> <Setter Property="Background" Value="{StaticResource MyBrush}" /> </Style> </DataGridTextColumn.HeaderStyle> </DataGridTextColumn> </DataGrid.Columns> </DataGrid> ``` 2. 使用绑定属性设置DataGrid.Columns的Background属性: ``` <DataGrid> <DataGrid.Columns> <DataGridTextColumn Header="Column 1" Binding="{Binding Column1}"> <DataGridTextColumn.HeaderStyle> <Style TargetType="DataGridColumnHeader"> <Setter Property="Background" Value="{Binding DataContext.Column1HeaderBackground, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" /> </Style> </DataGridTextColumn.HeaderStyle> </DataGridTextColumn> </DataGrid.Columns> </DataGrid> ``` 3. 使用样式设置DataGrid.Columns的Background属性: ``` <DataGrid> <DataGrid.Resources> <Style TargetType="DataGridColumnHeader"> <Setter Property="Background" Value="LightGray" /> </Style> </DataGrid.Resources> <DataGrid.Columns> <DataGridTextColumn Header="Column 1" Binding="{Binding Column1}" /> </DataGrid.Columns> </DataGrid> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值