Question

I have the next code where I defined a WPF toolkit datagrid control called dgQuery; I filled this one with information of a dataset, then I inserted a new checkbox column in dgQuery to check/uncheck some of the rows, I show part of my C# code:

dgQuery.DataContext = dS.Tables[0];

DataGridTemplateColumn cbCol = new DataGridTemplateColumn();
cbCol.Header = "Opc";
FrameworkElementFactory factory = new FrameworkElementFactory(typeof(CheckBox));
Binding bind = new Binding("IsSelected");
bind.Mode = BindingMode.TwoWay;
factory.SetValue(CheckBox.IsCheckedProperty, bind);
DataTemplate cellTemplate = new DataTemplate();
cellTemplate.VisualTree = factory;
cbCol.CellTemplate = cellTemplate;
dgQuery.Columns.Insert(0, cbCol);

After checking/unchecking into the new checkbox column of the dgQuery rows I will click a button to save into a database only the rows I checked. The question is, how can I develop the loop for reading all the rows of dgQuery and the condition that will let me know which rows have the checkbox checked/unchecked? Help me with an example, please.

Thanks!!

Was it helpful?

Solution

this will return a 'row' in your datagrid

public IEnumerable<Microsoft.Windows.Controls.DataGridRow> GetDataGridRows(Microsoft.Windows.Controls.DataGrid grid)
    {
        var itemsSource = grid.ItemsSource as IEnumerable;
        if (null == itemsSource) yield return null;
        foreach (var item in itemsSource)
        {
            var row = grid.ItemContainerGenerator.ContainerFromItem(item) as Microsoft.Windows.Controls.DataGridRow;
            if (null != row) yield return row;
        }
    }

in wpf datagrid, rows are ItemSource.items... no Rows property!

Hope this helps...

OTHER TIPS

 var row = GetDataGridRows(dataGrid1);
 /// go through each row in the datagrid
            foreach (Microsoft.Windows.Controls.DataGridRow r in row)
            {
                DataRowView rv = (DataRowView)r.Item;

                // Get the state of what's in column 1 of the current row (in my case a string)
                string t = rv.Row[1].ToString();


            }

Not sure if this is helpful because it assumes a different approach than what you started with, but rather than working directly with the grid, you could bind it to an ObservableCollection of objects that have properties for each column. If you add a bool property in your object for "Selected" and bind the checkbox column to it, you can then query the collection at any time for what is currently selected, like this:

 List<MemberEntity> selectedItems = 
            new List<MemberEntity>(from memberEntity in _memberEntities 
                                   where memberEntity.Selected == true 
                                   select memberEntity);

        //now save selectedItems to the database...

So MemberEntity is a class that has a property for each of the columns in your grid, including a bool called Selected for the checkbox column. _memberEntities is an ObservableCollection of MemberEntity instances. The grid's ItemSource property is bound to _memberEntities and each of its column's Binding properties are bound to a property in MemberEntity like this, assuming that Selected and Name are properties in MemberEntity:

<tk:DataGrid ItemsSource="{Binding _memberEntities}">
        <tk:DataGrid.Columns>
            <tk:DataGridCheckBoxColumn Binding="{Binding Path=Selected}" />
            <tk:DataGridTextColumn Binding="{Binding Path=Name}" />
        </tk:DataGrid.Columns>
</tk:DataGrid>
//Looping thought datagrid rows & loop though cells and alert cell values

var row = GetDataGridRows(DataGrid_Standard);
/// go through each row in the datagrid 
foreach (Microsoft.Windows.Controls.DataGridRow r in row)
{
    DataRowView rv = (DataRowView)r.Item;
    foreach (DataGridColumn column in DataGrid_Standard.Columns)
    {
        if (column.GetCellContent(r) is TextBlock)
        {
            TextBlock cellContent = column.GetCellContent(r) as TextBlock;
            MessageBox.Show(cellContent.Text);
        }
        else if (column.GetCellContent(r) is CheckBox)
        {
            CheckBox chk = column.GetCellContent(r) as CheckBox;
            MessageBox .Show (chk.IsChecked.ToString());
        }                      
    }
} 

public IEnumerable<Microsoft.Windows.Controls.DataGridRow> GetDataGridRows(Microsoft.Windows.Controls.DataGrid grid)
{
    var itemsSource = grid.ItemsSource as IEnumerable;
    if (null == itemsSource) yield return null;
    foreach (var item in itemsSource)
    {
        var row = grid.ItemContainerGenerator.ContainerFromItem(item) as Microsoft.Windows.Controls.DataGridRow;
        if (null != row) yield return row;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top