0
votes

I am working on an application that validates cells in DataGrid in WPF. If there is an error in a cell, for example, I am making the cell editable. However, the changed data is not getting binded to the data-grid's ItemsSource. The following is the code that I am using to make the cell editable when there is error:

DataGridRow gridRow = dgInventory.ItemContainerGenerator.ContainerFromIndex(row) as DataGridRow;

if (gridRow != null)
{
    DataGridCell cell = dgInventory.Columns[column].GetCellContent(gridRow).Parent as DataGridCell;

    cell.BorderBrush = Brushes.Red;
    cell.IsEditing = true;

    cell.ToolTip = tooltip;
}

Once the grid loads in the page, I can now edit the cells with error. However, when I access the ItemsSource for the DataGrid, it still shows the same old data. The DataGrid code in XAML is this:

<DataGrid Name="dgInventory" ScrollViewer.CanContentScroll="False" IsManipulationEnabled="True" CellEditEnding="dgInventory_CellEditEnding" IsReadOnly="True" />

Can you please provide me a way to edit the cells in the DataGrid. Thanking you in advance.

1
Can you please include the DataGridColumn XAML as well - it will help to see how the columns are being databound, and what type of columns they are. Also, does your CellEditEnding ever get fired? - Brian S
There is no extra DataGridColumn XAML. The data-grid's itemssource is set as dataset.Tables[0].DefaultView, and the grid shows up. Also, the CellEditEnding does get fired after we edit a cell and then we change focus by clicking on an area other than the cell. - Nishanth Reddy

1 Answers

0
votes

Maybe problem in IsReadOnly="True"? Try

DataGridRow gridRow = dgInventory.ItemContainerGenerator.ContainerFromIndex(row) as DataGridRow;

if (gridRow != null)
{
    DataGridCell cell = dgInventory.Columns[column].GetCellContent(gridRow).Parent as DataGridCell;

    cell.BorderBrush = Brushes.Red;
    cell.IsEditing = true;
    dgInventory.IsReadOnly = false;

    cell.ToolTip = tooltip;
}

And than in dgInventory_CellEditEnding set:

...
IsReadOnly = true;
...