-1

I have the following wpf datagrid:

<DataGrid x:Name="dataGridOrderItems" Margin="4,38,2,70"
VerticalGridLinesBrush="LightGray"  HorizontalGridLinesBrush="LightGray" AlternatingRowBackground="Beige"  AlternationCount="2" 
SelectionMode="Single" SelectionUnit="FullRow" 
AutoGenerateColumns="False" IsReadOnly="False" SelectionChanged="dataGridOrderItems_SelectionChanged">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Resource Id" Binding="{Binding ResourceId}" />
        <DataGridTextColumn Header="Resource Name" Binding="{Binding DisplayTitle}" />
        <DataGridTextColumn Header="Quantity" Binding="{Binding Quantity}" />
        <DataGridTextColumn Header="Type" />
        <DataGridTextColumn Header="Location" />
        <DataGridTextColumn Header="Order Id" Binding="{Binding OrderId}" />
        <DataGridTextColumn Header="Order Date" Binding="{Binding OrderDate, StringFormat=\{0:d\}}" />
        <DataGridTemplateColumn Header="Select" CellTemplate="{StaticResource template}"/>
    </DataGrid.Columns>
</DataGrid>

I've been trying to work out how I can iterate over the items in the grid and only process for the ones that are selected which I've done by using cast and and a class with properties that match the column bindings.

I'm trying to iterate using the following:

    foreach (
        var orderItem in
            this.dataGridOrderItems.ItemsSource.Cast<CustomerOrdersEntity>()
                .Where(item => item.IsChecked)
                .Where(item => item.DisplayTitle != null))
    {
        MessageBox.Show(orderItem.DisplayTitle);
    }

With the following class that I'm trying to use for casting:

public class CustomerOrdersEntity
{
    public string ResourceId { get; set; }
    public string DisplayTitle { get; set; }
    public string Quantity { get; set; }
    public string OrderId { get; set; }
    private DateTime _orderDate;
    public DateTime OrderDate
    {
        get
        {
            return Convert.ToDateTime(this._orderDate, new CultureInfo("hr-HR")).Date;
        }
        set
        {
            this._orderDate = value.Date;
        }
    }
    public bool IsChecked { get; set; }
}

However I'm being told that this is an invalid cast.

The datagrid was populated with a SqlCommand and stored procedure using the Dataview collection:

public DataView LoadOrders(int customerId, string status)
{
    using (var con = new SqlConnection(ConnectionString))
    {
        using (var cmd = new SqlCommand("RetreiveOrderSummaryByCustomerId", con))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@CustomerId", SqlDbType.VarChar).Value = customerId;
            cmd.Parameters.Add("@Status", SqlDbType.VarChar).Value = status;
            using (var adapter = new SqlDataAdapter(cmd))
            {
                DataTable dt = new DataTable();

                adapter.Fill(dt);
                con.Close();

                return dt.DefaultView;
            }
        }
    }
}

I'm populating the datagrid by using the code above and binding it as a itemssource using the following:

dataGridOrderItems.ItemsSource = data.LoadOrders(s, ComboBoxStatus.Text);

I think I might be tackling this in the complete wrong direction though and I'm wondering what would be the best approach for iterating only over the checked items in a WPF datagrid?

Michael A
  • 9,480
  • 22
  • 70
  • 114

1 Answers1

3

You should bind your DataGrid to a collection of objects and work with the underlying ItemsSource instead. Once you do that the task you are struggling with will become trivial because you'll be able to iterate over a collection of objects instead and perform any type of processing based on the object's Properties values.

Dean Kuga
  • 11,878
  • 8
  • 54
  • 108
  • I'm actually doing that with the contents of the stored procedure. This looks like 'dataGridOrderItems.ItemsSource = data.LoadOrders(s, ComboBoxStatus.Text);'. I believe there's a cast needed to access it still? – Michael A Oct 31 '16 at 23:16