Search code examples
c#wpfwpfdatagrid

WPF, work with datafrom a datagrid, independent on datagrid


I need get data from the datagrid and work with these data independent on the datagrid.

I wrote in XAML:

<Window x:Class="DatagridExample1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:DatagridExample1"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<Grid>
    <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Height="26" Margin="10,0,0,0" VerticalAlignment="Top" Width="774" Click="button_Click"/>
    <DataGrid x:Name="dg" HorizontalAlignment="Left" Height="379" Margin="10,31,0,0" VerticalAlignment="Top" Width="774" SelectionChanged="dg_SelectionChanged"  AutoGenerateColumns="False" SelectionUnit="CellOrRowHeader" CanUserAddRows="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Reference" Binding="{Binding Reference}" IsReadOnly="True"/>
            <DataGridTextColumn Header="Value" Binding="{Binding Value}" Width="*"/>
            <DataGridTextColumn Header="PartName" Binding="{Binding PartName}" Width="*"/>
         </DataGrid.Columns>
    </DataGrid>         

</Grid>

The written Class Part is as follows:

public class Part
{
    public Part()
    {
    }

    public string Reference { get; set; }
    public string Value { get; set; }
    public string PartName { get; set; }
}

and

after that, I wroted a collection with object of Part, fill these collection and push it in the datagrid.

    public  List<Part> list;
    public MainWindow()
    {
        Part p1 = new Part() { Reference = "R1", Value = "10R", PartName = "10R 0402" };

        Part p2 = new Part() { Reference = "R2", Value = "10R", PartName = "10R 0402" };
        Part p3 = new Part() { Reference = "R3", Value = "10R", PartName = "10R 0402" };
        list = new List<Part>(){};
        list.Add(p1);
        list.Add(p2);
        list.Add(p3);
        list.Add(p1);

        InitializeComponent();

        dg.ItemsSource = list;        

    }

This part is OK. After run program I can see my table:(see image in link) Main window: correct table

After that a I clicked on button and run this code:

    private void button_Click(object sender, RoutedEventArgs e)
    {
        List<Part> oldList = (dg.ItemsSource as List<Part>);
        Part[] p = oldList.ToArray();

        p[0].Reference += p[1].Reference;


    }

It looks like, OK. I worked only with array "p", I didn't worked with "dg" But when I sort columns in table. I am really confused, because data in "dg" was change. How and Why? How can I fix this. Because I need to work with array independent on the datagrid.

Why data in dg changed, when i didnt work with datagrid?


Solution

  • Well this is happening because you are altering the objects themselves.

    It doesn't matter that you make a new List because it is a list to the same references in the memory. Once you change any value, it is reflected in the grid showing those objects.

    If you want to "play" with those objects without those changing reflecting the grid, you need new objects. You need to copy them to a different reference.

    Check this: Creating a copy of an object in C#