here is the solution in Wpf:
design:
<pre lang="HTML">
<window x:class="WpfApplication3.MainWindow" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<grid>
<stackpanel orientation="Vertical">
<datagrid autogeneratecolumns="False" height="200" horizontalalignment="Center" verticalalignment="Center" name="dataGrid1" width="auto">
<datagrid.columns>
<datagridtemplatecolumn width="80">
<datagridtemplatecolumn.header>
<checkbox content="Select All" x:name="chkSelectAll" checked="chkSelectAll_Checked" unchecked="chkSelectAll_Unchecked">
<datagridtemplatecolumn.celltemplate>
<datatemplate>
<checkbox x:name="chkIsChecked" ischecked="{Binding IsChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" horizontalalignment="Center">
<datagridtextcolumn header="ID" binding="{Binding ID}" width="60" isreadonly="True">
<datagridtextcolumn header="Name" binding="{Binding Name}" width="140*" isreadonly="True">
<datagridtextcolumn header="Amount" binding="{Binding Amount, StringFormat=N2}" width="140" isreadonly="True">
<stackpanel orientation="Horizontal" horizontalalignment="Right">
<textblock text="Total" margin="0,0,40,0" fontsize="14">
<textblock name="txtTotal" text="0.0" margin="0,0,40,0" fontsize="14">
Code Behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.ComponentModel;
namespace WpfApplication3
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
List<item> items = new List<item>();
public MainWindow()
{
InitializeComponent();
items = InitializeGridValues();
dataGrid1.ItemsSource = items;
foreach (Item item in items)
{
item.PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
}
}
void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "IsChecked")
{
txtTotal.Text = items.Where(i => i.IsChecked == true).ToList<item>().Sum(i => i.Amount).ToString();
}
}
List<item> InitializeGridValues()
{
List<item> items = new List<item>();
items.Add(new Item { ID = 1, Name = "A", Amount = 200, IsChecked = false });
items.Add(new Item { ID = 2, Name = "B", Amount = 150, IsChecked = false });
items.Add(new Item { ID = 3, Name = "C", Amount = 134, IsChecked = false });
items.Add(new Item { ID = 4, Name = "D", Amount = 550, IsChecked = false });
items.Add(new Item { ID = 5, Name = "E", Amount = 430, IsChecked = false });
items.Add(new Item { ID = 6, Name = "F", Amount = 79, IsChecked = false });
items.Add(new Item { ID = 7, Name = "G", Amount = 100, IsChecked = false });
return items;
}
}
public class Item : INotifyPropertyChanged
{
private int _ID; // { get; set; }
private string _Name; // { get; set; }
private double _Amount; // { get; set; }
private bool _IsChecked;// { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(PropertyChangedEventArgs e)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, e);
}
protected void OnPropertyChanged(string propertyName)
{
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
public int ID
{
get { return _ID; }
set {
if (value != _ID)
_ID = value;
OnPropertyChanged("ID");
}
}
public string Name {
get { return _Name; }
set {
if (value != _Name)
_Name = value;
OnPropertyChanged("Name");
}
}
public double Amount {
get { return _Amount; }
set {
if (value != _Amount)
_Amount = value;
OnPropertyChanged("Amount");
}
}
public bool IsChecked {
get { return _IsChecked; }
set {
if (value != _IsChecked)
_IsChecked = value;
OnPropertyChanged("IsChecked");
}
}
}
}
feel free to ask if any doubt. thanks