Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
hi,

i want to use a data gridview with checkbox for selection of row and multiple rows.
for example: i have four column in my gridview. first column is checkbox.
checkbox id name amount
checkbox 1 abc 20
checkbox 2 xcv 40
checkbox 3 def 20

in my code i need to perform few task

1. when we click on single row, then a textbox will show the amount. and this amount will change according to checkbox click by the user

2. user can check more than one row at a time. so the changes reflect with sum of rows amount in textbox.

3. user can also select column header checkbox to select all the checkboxes and amount of all the rows will reflect on textbox.


please help me out. i want code to solve this problem.

thank you
Posted
Comments
Sinisa Hajnal 20-Jul-15 7:10am    
What have you tried? What code did you write? Which part of your code doesn't work?
[no name] 20-Jul-15 9:17am    
"Gimme code" is not a question or a problem description.

1 solution

here is the solution in Wpf:

design:

HTML
<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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900