0

i have a datagrid in wpf, i have multiple rows(items) in that datagrid and have one checkbox column in each row. i want to check in all rows if the checkbox is checked in any row then perform action below is my code. Thanks!

WPF Code

<DataGrid CanUserAddRows="False" AutoGenerateColumns="False"
                  CellEditEnding="SaveDeliveryValue" LoadingRow="DataGrid_LoadingRow"
                  Name="ViewOrdersGrid" HorizontalAlignment="Center" Margin="0,10,0,0" 
                  VerticalAlignment="Top" Height="278" Width="520" BorderBrush="#FFA0A0A0">
            <DataGrid.Columns>
                <DataGridTextColumn  Header="Order No" Width="115" Binding="{Binding Path=BONo, Mode=OneWay}" />
                <DataGridTextColumn Header="Order Date" Width="100" Binding="{Binding Path=BODate, Mode=OneWay, StringFormat=d}" />
                <DataGridTextColumn Header="Total Amount" Width="100" Binding="{Binding Path=BOTotal, Mode=OneWay}" />
                <DataGridTextColumn Header="Total Bikes" Width="100" Binding="{Binding Path=BOTatalBikes, Mode=OneWay}" />
                <DataGridCheckBoxColumn Header="Delivered" x:Name="DeliveryValueCheck" Width="70" Binding="{Binding Path=BODelivered, Mode=TwoWay}" />
            </DataGrid.Columns>
        </DataGrid>

C# code

private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            for (int i = 0; i < ViewOrdersGrid.Items.Count; i++)
            {
                CheckBox mycheckbox = ViewOrdersGrid.Columns[4].GetCellContent(ViewOrdersGrid.Items[i]) as CheckBox;
                if (mycheckbox.IsChecked == true)
                {
                    MessageBox.Show("Checked");
                }

            }
        }
1
  • 1
    Don't use procedural code to "read" UI elements' properties in WPF. UI is not Data. Create a proper ViewModel and use DataBinding instead. Commented Sep 23, 2013 at 14:24

1 Answer 1

1

You're already using MVVM, I can see by the bindings, so you're off to a good start. Now, because MVVM allows a very tight relationship between the UI and the data, we can inference that if we can traverse the visual tree for the checked property on a given object, we should also be able to traverse the data for such a property. So your C# code should look as follows (assuming in your code that DataGrid's ItemsSource is bound to a collection (lets call it MyItems):

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    var viewModel = (ViewModelType)this.DataContext;

    foreach(var item in viewModel.MyItems)
    {
        if(item.BODelivered)
        {
            MessageBox.Show("Checked");
        }
    }
}

This example assumes that (because the rest of your example is using bindings appropriately) that your grid is bound to something (we called it MyItems). If you need to see how this works (meaning you haven't implemented it as MVVM and FOOLED ME) then consider the following:

This is your XAML

<DataGrid CanUserAddRows="False" AutoGenerateColumns="False"
          CellEditEnding="SaveDeliveryValue" LoadingRow="DataGrid_LoadingRow"
          Name="ViewOrdersGrid" HorizontalAlignment="Center" Margin="0,10,0,0" 
          VerticalAlignment="Top" Height="278" Width="520" BorderBrush="#FFA0A0A0"
          ItemsSource="{Binding MyItems}">
    <DataGrid.Columns>
        <DataGridTextColumn  Header="Order No" Width="115" Binding="{Binding Path=BONo, Mode=OneWay}" />
        <DataGridTextColumn Header="Order Date" Width="100" Binding="{Binding Path=BODate, Mode=OneWay, StringFormat=d}" />
        <DataGridTextColumn Header="Total Amount" Width="100" Binding="{Binding Path=BOTotal, Mode=OneWay}" />
        <DataGridTextColumn Header="Total Bikes" Width="100" Binding="{Binding Path=BOTatalBikes, Mode=OneWay}" />
        <DataGridCheckBoxColumn Header="Delivered" x:Name="DeliveryValueCheck" Width="70" Binding="{Binding Path=BODelivered, Mode=TwoWay}" />
    </DataGrid.Columns>
</DataGrid>

This is your data structure

public class MyObject
{
    public int BONo { get; set; }
    public DateTime BODate { get; set; }
    public int BOTotal { get; set; }
    public int BOTatalBikes { get; set; }
    public bool BODelivered { get; set; }
}

This is your *.xaml.cs file

// this is the constructor for your view (MyWindow.xaml.cs)
private MyWindow( )
{
    this.DataContext = new MyViewModel( );
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    var viewModel = (ViewModelType)this.DataContext;

    foreach(var item in viewModel.MyItems)
    {
        if(item.BODelivered)
        {
            MessageBox.Show("Checked");
        }
    }
}

This is your view model (MyViewModel.cs)

public class MyViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    public void OnPropertyChanged(string property)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(property));
    }

    private ObservableCollection<YourObjectTypeHere> _myItems;
    public ObservableCollection<YourObjectTypeHere> MyItems
    {
        get
        {
            return _myItems;
        }
        set
        {
            _myItems = value;
            OnPropertyChanged("MyItems");
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.