Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a DataGrid with two columns , one theme is checkbox column and the other is text column .

I want to iterate through DataGrid Rows and check if CheckBox Cell was Checked , then add this row to list of employee

it's in winforms all to do that:
foreach(var r in DataGridRows)
{
if(r.cell[0].value == true)
{
// TODO
}

}

What I have tried:

for (int i = 0; i < dg.Items.Count; i++)
    {
        DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(i);
            for (int j = 0; j < dg.Columns.Count; j++)
            {
                var cellContent = dg.Columns[j].GetCellContent(row) as CheckBox;
               
            }
    }
Posted
Updated 1-Jan-18 18:18pm

Typically speaking you want to avoid working through UI elements directly unless it's UI specific, and in your problem this is about a business relationship so you really want it in the view model. In this example I would recommend some sort of EmployeeViewModel with an IsAdded boolean property (of course make the property name meaningful to what it's added too).

I would have the CheckBox be read only and have a button below the grid bound to a command on the collection that's something like ICommand AddEmployeeToCollectionCommand passing the EmployeeViewModel (I prefer the DelegateCommand implementation in PRISM). Of course you're also need to have a remove button or use the button as an Add/Remove (you can use a converter or trigger on the button to change the name appropriately).

If you really want to handle the click event then I recommend handling the click event at the grid level (these are routed commands). That way the grid/view can manipulate the higher level view model (hopefully there is one) by toggling the status of the source data context.
 
Share this answer
 
I recommend an alternative - use a bound datagrid where the items have the two fields you want shown in the grid. For example:
C#
public class DataItem : DependencyObject
{
    public static readonly DependencyProperty DisplayTextProperty = DependencyProperty.Register("DisplayText", typeof(string), typeof(DataItem), new PropertyMetadata(default(string)));
    public string DisplayText { get { return (string)GetValue(DisplayTextProperty); } set { SetValue(DisplayTextProperty, value); } }
    public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.Register("IsSelected", typeof(bool), typeof(DataItem), new PropertyMetadata(default(bool)));
    public bool IsSelected { get { return (bool)GetValue(IsSelectedProperty); } set { SetValue(IsSelectedProperty, value); } }
}

Using a DependencyObject ensures the DataGrid maps the properties properly.
Now just create a list of these DataItems and assign the list to the DataGrid's ItemSource
C#
...
List<DataItem> list = new List<DataItem>();
...fill list with items ...
dataGrid.ItemsSource = list;
...


To check through the required ones, just scan the list either using a foreach loop or a LINQ selector.
C#
foreach(DataItem item in list)
{
  if (item.IsSelected) 
  {
   ... do your stuff here ...
  }
}
 
Share this answer
 

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