Search code examples
c#wpfdatagriddatagridcell

Returns null from edited cell of DataGrid


I create a simple DataGrid with editable cells in WPF. But i am getting null data from the edited cell. I am newbie in WPF. What is the problem?

When I attempt to change the Folder cell, it is changed as seen in the image.

This is the result!

The Codes,

namespace MyApp
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            List<Product> tmpProducts = new List<Product>();

            tmpProducts.Add(new Product {
                id = 1, title = "Product A", folder = null,
            });
            tmpProducts.Add(new Product {
                id = 2,  title = "Product B", folder = null,
            });

            productGrid.ItemsSource = tmpProducts;
        }

        private void selectFolder(object sender, RoutedEventArgs e)
        {
            // selected folder from dialog, etc. then,
            string selectedPath = "C:\\myFolder";

            object item = productGrid.SelectedItem;
            (productGrid.SelectedCells[2].Column.GetCellContent(item) as TextBlock).Text = selectedPath;
        }

        private void updateProducts(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < productGrid.Items.Count; i++)
            {
                Product prd = (Product)productGrid.Items[i];
                Console.WriteLine(prd.folder);
            }
        }
    }


    class Product
    {
        public int id { get; set; }
        public string title { get; set; }
        public string folder { get; set; }
    }
}
<Window x:Class="MyApp.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:MyApp"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid x:Name="productGrid" AutoGenerateColumns="False" CanUserAddRows="False" Margin="0,0,0,36">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding id}" Header="ID"/>
                <DataGridTextColumn Binding="{Binding title}" Header="Title" Width="*"/>
                <DataGridTextColumn Binding="{Binding folder}" Header="Foler" Width="*"/>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Click="selectFolder" Content="Browse.." Width="100"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

        <Button Click="updateProducts" Content="Save" HorizontalAlignment="Left" Margin="432,289,0,0" VerticalAlignment="Top" Width="75"/>
    </Grid>
</Window>

Solution

  • You are setting the "folder" property of your products to null and then you are never setting it again. You are just changing the text of the DataGridCell. You need to set the folder property of your products as well before you try to get it on the updateProducts event. Like this:

    private void selectFolder(object sender, RoutedEventArgs e)
    {
        // selected folder from dialog, etc. then,
        string selectedPath = "C:\\myFolder";
    
        object item = productGrid.SelectedItem;
    
        (item as Product).folder = selectedPath; //set the folder property of your Product item
    
        (productGrid.SelectedCells[2].Column.GetCellContent(item) as TextBlock).Text = selectedPath;
    }