How to validate and entry on a datagrid that is bound to an adapter

Posted by Ziv on Stack Overflow See other posts from Stack Overflow or by Ziv
Published on 2010-06-12T22:01:27Z Indexed on 2010/06/12 22:02 UTC
Read the original article Hit count: 307

Filed under:
|
|
|
|

Hi, I've been using C# for a while and began a program now to learn WPF-which means I know almost nothing of it.

I used this tutorial as a guide to do what I wanted to do (which is binding a database to a datagrid), after a hard struggle to add the adapter as the source of the datagrid I now want to enable editing with validation on some of the cells.

My problem is that the data is sent straight from the adapter and not through an object collection (I had a hard time getting to this situation, see the first half of the tutorial on how to bind the adapter and dataset through the resources) but the tutorial doesn't show a way to validate the datagrid if the data is sent through an adapter-only through a collection.

To make it clear-how do I validate input in a datagrid that is bound to an adapter through a resource?

The relevant code: (XAML)

<Window.Resources>
    <ObjectDataProvider x:Key="DiscsDataProvider" ObjectType="{x:Type local:DiscsDataProvider}" />
    <ObjectDataProvider x:Key="Discs" ObjectInstance="{StaticResource ResourceKey=DiscsDataProvider}" MethodName="GetDiscs" />

    <Style x:Key="CellEditStyle" TargetType="{x:Type TextBox}">
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="Padding" Value="0"/>
        <Setter Property="Background" Value="Yellow"/>
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">
                <Setter Property="ToolTip"
                        Value="{Binding RelativeSource={RelativeSource Self},
                                Path=(Validation.Errors)[0].ErrorContent}"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

For the datagrid:

<Grid Width="auto" Height="auto">
    <DockPanel DataContext="{Binding Source={StaticResource ResourceKey=Discs}}">
        <DataGrid Margin="12,0,0,12" Name="View_dg" HorizontalAlignment="Left" Width="533" Height="262" VerticalAlignment="Bottom" ItemsSource="{Binding}" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" CanUserResizeColumns="True">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Path=ContainerID}" CanUserReorder="False" CanUserResize="True" CanUserSort="True"  EditingElementStyle="{StaticResource CellEditStyle}" IsReadOnly="False" Header="Container" />
                <DataGridTextColumn Binding="{Binding Path=ID}"          CanUserReorder="False" CanUserResize="True" CanUserSort="True"  EditingElementStyle="{StaticResource CellEditStyle}" IsReadOnly="True"  Header="ID" />
                <DataGridTextColumn Binding="{Binding Path=Title}"       CanUserReorder="False" CanUserResize="True" CanUserSort="True"  EditingElementStyle="{StaticResource CellEditStyle}" IsReadOnly="False" Header="Title" />
                <DataGridTextColumn Binding="{Binding Path=SubTitle}"    CanUserReorder="False" CanUserResize="True" CanUserSort="False" EditingElementStyle="{StaticResource CellEditStyle}" IsReadOnly="False" Header="Sub Title" />
                <DataGridTextColumn Binding="{Binding Path=Type}"        CanUserReorder="False" CanUserResize="True" CanUserSort="True"  EditingElementStyle="{StaticResource CellEditStyle}" IsReadOnly="False" Header="Type" />
                <DataGridTextColumn Binding="{Binding Path=Volume}"      CanUserReorder="False" CanUserResize="True" CanUserSort="False" EditingElementStyle="{StaticResource CellEditStyle}" IsReadOnly="False" Header="Volume" />
                <DataGridTextColumn Binding="{Binding Path=TotalDiscs}"  CanUserReorder="False" CanUserResize="True" CanUserSort="False" EditingElementStyle="{StaticResource CellEditStyle}" IsReadOnly="False" Header="Total Discs" />
            </DataGrid.Columns>
        </DataGrid>
    </DockPanel>

and C#:

public class DiscsDataProvider
{
    private DiscsTableAdapter adapter;

    private DB dataset;

    public DiscsDataProvider()
    {
        dataset = new DB();
        adapter = new DiscsTableAdapter();
        adapter.Fill(dataset.Discs);
    }

    public DataView GetDiscs()
    {
        return dataset.Discs.DefaultView;
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about wpf