简体   繁体   中英

Accessing a particular row/column in a dataGrid in WPF

Recently I have just started to learn WPF. Although most of my experience with development has come from Win Forms, I have managed to transition to WPF fairly smoothly.

                                OVERVIEW

I am writing a program that compares information in two separate dataTables and reflects the changes made in a dataGrid.

The dataTables represent a list of "old Information" and "New Information". After the dataTables are compared I wish to highlight particular cells based on whether a change has been made in the "new information table".

An example would be if the first cell in a dataGrid ([0][0]) contained '3' from the "Old Information" dataTable and if the same cell ([0][0]) in the "New Information" dataTable contained '5'. I would wish to change the background color to say, yellow, to reflect this change.

                                 PROBLEM

I am currently working with two dataGrid objects, which are being filled, and representing both dataTables ("Old Information" and "New Information"). The problem I'm having is that I can't seem to access a particular row or column nicely. I would like to be able to access a particular row/ cell so I could change the background color via code.

                                 Comments

I remember when I worked with Win Forms you could do something along the lines of dataGrid1.Rows[rowNumber].Columns[ColumnNumber] to access a particular row/column.

From what I have read so far about WPF dataGrids they are more object based or something like that, making the process of accessing a particular row/column a little different/tedious. If you happen to know how one would go about accessing the row/ cell content in a dataGrid in WPF please let me know. Any simple examples would be greatly appreciated.

You are correct that DataGrids are very much geared towards data binding, and it can be difficult to programmatically access and manipulate the rows, columns and cells within. Going down the MVVM route is the preferred approach when using WPF. In your case you can drive the cell colours using the underlying data rather than trying to write code-behind code to achieve this.

MVVM is a big subject, but as a flavour of what might be involved, you would typically create a "model" class representing the entity that you want to appear in the grid's rows. Your view-model will expose a collection of these objects, which are bound to the grid and rendered as individual rows (normally you would manually define columns in the XAML, binding them to individual properties of the model class).

It's difficult to provide an example without knowing what your particular grids are displaying. But let's say you are listing customer details - the first column might be the name, the next column their address, and so on. One solution may be to include additional boolean properties on the model class to indicate whether a particular property value differs from its "old" counterpart. These properties could be utilised by data triggers to change the cell background colour, with the data triggers residing within CellStyles that are applied to the data grid columns.

Quick and dirty code sample off the top of my head, so may not be 100% correct:

public class Customer
{
    public string CustomerName {get; set;}
    public string CustomerAddress {get; set;}
    public bool CustomerNameDiffers {get; set;}
    public bool CustomerAddressDiffers {get; set;}
}

public class MyViewModel
{
    public ObservableCollection<Customer> Customers {get; set;}

    //etc..
}

XAML:

<Window.Resources>
   <Style x:Key="CustomerNameCellStyle"
          TargetType="DataGridTextColumn">
       <Style.Triggers>
           <DataTrigger Binding="{Binding CustomerNameDiffers}"
                        Value="True">
               <Setter Property="Background" 
                       Value="Yellow" />
           </DataTrigger>
       </Style.Triggers>
   </Style>
</Window.Resources>

<DataGridTextColumn Binding="{Binding CustomerName}" 
                    CellStyle="{StaticResource CustomerNameCellStyle}"
                    ... />

I realise it's a bit of a generalised answer but MVVM is a big topic, but it's well worth gaining an understanding if you are to make the most of WPF and ditch the code-behind habits from Winforms!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM