How to validate datagridcell value based on previous value

1.4k Views Asked by At

I have created a datagrid of multiple rows and columns. One of the columns is a list of field sizes that the user can change.

I am checking the new value against the old value and if the new value is less then the old value I am telling the user this is not valid and then I want to put the old value back and reset the focus to that cell.

I have this line in my LostFocus event:

System.Windows.Controls.TextBox tbNewSize = 
    (System.Windows.Controls.TextBox)dtgCell.Content;

When I click on the cell, the LostFocus event is called and works fine. However when i try to refocus to the cell I get an error saying

"Unable to cast object of type 'System.Windows.Controls.TextBlock' to type 'System.Windows.Controls.TextBox'."

How do I correct this issue?

Here is my XAML code:

<DataGrid HeadersVisibility="Column" Name="dtGrid" Loaded="GridLoaded" AutoGenerateColumns="False" IsReadOnly="False" VirtualizingPanel.IsVirtualizing="False" Height="365" Width="530" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="54,74,0,0" BorderThickness="1,1,0,1" BorderBrush="Black">
<DataGrid.Columns>
    <DataGridTextColumn Header="Field" Binding="{Binding Field, Mode=TwoWay}" Width="209" IsReadOnly="True" />
    <DataGridTextColumn Header="Size" Binding="{Binding Size, Mode=TwoWay}" Width="89"/>
    <DataGridCheckBoxColumn Header="Right Justify" Binding="{Binding RightJustify, Mode=TwoWay}" Width="55" />
    <DataGridCheckBoxColumn Header="Left Justify" Binding="{Binding LeftJustify, Mode=TwoWay}"  Width="55"  />
    <DataGridCheckBoxColumn Header="Left Zero Fill" Binding="{Binding LeftZeroFill, Mode=TwoWay}" Width="55" />
    <DataGridCheckBoxColumn Header="Right Zero Fill" Binding="{Binding RightZeroFill, Mode=TwoWay}" Width="65" />
</DataGrid.Columns>
<DataGrid.ColumnHeaderStyle>
    <Style TargetType="DataGridColumnHeader">
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <TextBlock TextWrapping="Wrap" Text="{Binding}"></TextBlock>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</DataGrid.ColumnHeaderStyle>
<DataGrid.Resources>
    <Style TargetType="{x:Type DataGridCell}">
        <Style.Triggers>
            <Trigger Property="DataGridCell.IsSelected" Value="True">
                <Setter Property="Background" Value="#FF9DF3D6" />
                <Setter Property="Foreground" Value="#000000" />
            </Trigger>
        </Style.Triggers>
        <EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown" />
        <EventSetter Event="LostFocus" Handler="DataGridCell_OnCellLostFocus" />
    </Style>
</DataGrid.Resources>

Here is my c# code:

private void DataGridCell_OnCellLostFocus(object sender, RoutedEventArgs e)
{
    System.Windows.Controls.DataGridCell dtgCell = (System.Windows.Controls.DataGridCell)sender;

    if (dtgCell.Column.Header.ToString() == "Size")
    {
        System.Windows.Controls.TextBox tbNewSize = (System.Windows.Controls.TextBox)dtgCell.Content;
        Int32 intNewSize = Convert.ToInt32(tbNewSize.Text);
        Int32 intCurrSize = Convert.ToInt32(strFieldInfoOrig[dtGrid.Items.IndexOf(dtGrid.CurrentItem), 1]);

        if (intNewSize < intCurrSize)
        {
            string strMsg;

            strMsg = "New size, " + intNewSize.ToString() + " is smaller then the original size, " + intCurrSize.ToString();
            strMsg += Environment.NewLine;
            strMsg += "Due to potential data loss, this is not allowed.";
            System.Windows.MessageBox.Show(strMsg);
            //dtgCell.Content = intCurrSize.ToString();
            dtgCell.Focus();
        }
    }
}
3

There are 3 best solutions below

0
AnjumSKhan On

This is happening because DataGridTextColumn shows a TextBlock in normal mode and TextBox while editing. So, when this cell loses focus, DataGridTextColumn returns in normal mode, so its content would be a TextBlock and not TextBox and hence it shows exception.

So, try to cast into TextBlock and not TextBox.

8
bab7lon On

You could handle the CellEditEnding event.

<DataGrid AutoGenerateColumns="False"
          CellEditEnding="DataGrid_CellEditEnding"
          ...
          >
    <DataGrid.Columns>
        <DataGridTextColumn Header="Size" Binding="{Binding Size, Mode=TwoWay}" .../>

        ...

    </DataGrid.Columns>

    ...
    
</DataGrid>

Code behind

private void DataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) 
{
    if (e.EditAction == DataGridEditAction.Commit)
    {
        if (e.Column is DataGridBoundColumn)
        {
            DataGridBoundColumn column = (DataGridBoundColumn)e.Column;
            if (column.Header.ToString() == "Size")
            {
                string oldValue = e.Row.DataContext.GetType().GetProperty("Size")
                                   .GetValue(e.Row.DataContext).ToString();
                TextBox element = e.EditingElement as TextBox;
                string newValue = element.Text;
                int oldSize = int.Parse(oldValue);
                int newSize = int.Parse(newValue);
                if (newSize < oldSize)
                {
                    string strMsg = "New size, " + newValue + ", is smaller then the original size, "
                                  + oldValue + ".\nDue to potential data loss, this is not allowed.";
                    MessageBox.Show(strMsg);
                    element.Text = oldValue;
                    e.Cancel = true;
                }
            }
        }
    }
}

Setting e.Cancel = true keeps the cell in edit mode.

1
mm8 On

You are trying to cast a TextBlock to a TextBox and this obviously won't work. But if you simply try to always cast to a TextBlock like this:

System.Windows.Controls.TextBlock tbNewSize = (System.Windows.Controls.TextBlock)dtgCell.Content;

...this won't work either. This is because the Content of the cell may be a TextBox or a TextBlock depending on whether the cell is currently in edit mode or not.

What you could to is to use the as operator to try to cast to a TextBox and if the cast fails you then cast the Content property to a TextBlock:

private void DataGridCell_OnCellLostFocus(object sender, RoutedEventArgs e)
{
    System.Windows.Controls.DataGridCell dtgCell = (System.Windows.Controls.DataGridCell)sender;
    if (dtgCell.Column.Header.ToString() == "Size")
    {
        string text = null;
        System.Windows.Controls.TextBox tbNewSize = dtgCell.Content as System.Windows.Controls.TextBox;
        if (tbNewSize != null)
        {
            text = tbNewSize.Text;
        }
        else
        {
            System.Windows.Controls.TextBlock tb = dtgCell.Content as System.Windows.Controls.TextBlock;
            if (tb != null)
                text = tb.Text;
        }
        Int32 intNewSize = Convert.ToInt32(text);
        Int32 intCurrSize = Convert.ToInt32(strFieldInfoOrig[dtGrid.Items.IndexOf(dtGrid.CurrentItem), 1]);

        if (intNewSize < intCurrSize)
        {
            string strMsg;

            strMsg = "New size, " + intNewSize.ToString() + " is smaller then the original size, " + intCurrSize.ToString();
            strMsg += Environment.NewLine;
            strMsg += "Due to potential data loss, this is not allowed.";
            System.Windows.MessageBox.Show(strMsg);
            //dtgCell.Content = intCurrSize.ToString();
            dtgCell.Focus();
        }
    }
}