Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. How to check all rows in DataGrid by clicking Check all Button in WPF in C#

How to check all rows in DataGrid by clicking Check all Button in WPF in C#

Scheduled Pinned Locked Moved WPF
csharpwpftutorialquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    ChandrakanthGaddam
    wrote on last edited by
    #1

    HI, This is from Chandrakanth. Working on WPF Project. Actually i have one CheckButton called as CheckALL. I have one DataGrid. DataGrid Consist of some rows. I want to Click on that CheckAll Button than all the rows in DataGrid should check. How can i write that in WPF. Can any one tell me about that. Thanks and Regards Chandrakanth Responses

    P 1 Reply Last reply
    0
    • C ChandrakanthGaddam

      HI, This is from Chandrakanth. Working on WPF Project. Actually i have one CheckButton called as CheckALL. I have one DataGrid. DataGrid Consist of some rows. I want to Click on that CheckAll Button than all the rows in DataGrid should check. How can i write that in WPF. Can any one tell me about that. Thanks and Regards Chandrakanth Responses

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      You would have a boolean property on the data, let's call it Selected. Then you bind your checkbox to the property, and your code simply updates the data - leaving the binding engine to ensure that the checkbox is updated as a result. Don't try to do something like this in the UI, use the data to control it.

      "WPF has many lovers. It's a veritable porn star!" - Josh Smith

      As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

      My blog | My articles | MoXAML PowerToys | Onyx

      C 1 Reply Last reply
      0
      • P Pete OHanlon

        You would have a boolean property on the data, let's call it Selected. Then you bind your checkbox to the property, and your code simply updates the data - leaving the binding engine to ensure that the checkbox is updated as a result. Don't try to do something like this in the UI, use the data to control it.

        "WPF has many lovers. It's a veritable porn star!" - Josh Smith

        As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

        My blog | My articles | MoXAML PowerToys | Onyx

        C Offline
        C Offline
        ChandrakanthGaddam
        wrote on last edited by
        #3

        HI, Pete O'Hanlon, Thaks for the early reply. Once Again from Chandrakanth. Actually i have bind the data to the DataGrid Control. I have 4 rows in that dataGrid with every row containing CheckBox in the first Column. My Problem How to Check all the rows in the DataGrid, when i click on CHECKALL button.Actully i was tried with some code to check all Rows in DataGrid. But Unable to that. i need help on that. If you have any code on that Please reply to me. Thanks and Regards Chandrakanth

        P 1 Reply Last reply
        0
        • C ChandrakanthGaddam

          HI, Pete O'Hanlon, Thaks for the early reply. Once Again from Chandrakanth. Actually i have bind the data to the DataGrid Control. I have 4 rows in that dataGrid with every row containing CheckBox in the first Column. My Problem How to Check all the rows in the DataGrid, when i click on CHECKALL button.Actully i was tried with some code to check all Rows in DataGrid. But Unable to that. i need help on that. If you have any code on that Please reply to me. Thanks and Regards Chandrakanth

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          I've already told you how to do it. Don't try to programatically check items in the grid, use the model to do this. Suppose you have a model that looks like this:

          public class MyItem : INotifyPropertyChanged
          {
          private bool _selected;
          private string _name;
          private event PropertyChangedEventHandler _changed;
          public event PropertyChangedEventHandler Changed
          {
          add { _changed += value; }
          remove { _changed -= value; }
          }

          public bool Selected
          {
          get { return _selected; }
          set
          {
          if (_selected != value)
          {
          _selected = value;
          OnChanged("Selected");
          }
          }
          }
          // Name code ommitted for brevity...
          }
          public class MyViewModel
          {
          private ObservableCollection<MyItem> _items = new ObservableCollection<MyItem>();

          public void HandleCheckAll
          {
          foreach (MyItem item in _items)
          {
          item.Selected = true;
          }
          }

          public ObservableCollection<MyItem> Items
          {
          get { return _items; }
          }

          // Some code to populate the items would need to go here....
          }

          All you need then is a command that calls HandleCheckAll and the grid binding to the MyViewModel.Items. The checkbox is bound (two-way) to Selected, and job's done.

          "WPF has many lovers. It's a veritable porn star!" - Josh Smith

          As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

          My blog | My articles | MoXAML PowerToys | Onyx

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups