Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to validate an entry on a datagrid that is bound to an adapter
    primarykey
    data
    text
    <p>I've been using C# for a while and began a program now to learn WPF-which means I know almost nothing of it.</p> <p>I used <a href="http://www.codeproject.com/KB/WPF/WPFDataGridExamples.aspx" rel="nofollow noreferrer">this</a> 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.</p> <p>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.</p> <p>To make it clear-how do I validate input in a datagrid that is bound to an adapter through a resource?</p> <p>The relevant code: (XAML)</p> <pre><code>&lt;Window.Resources&gt; &lt;ObjectDataProvider x:Key="DiscsDataProvider" ObjectType="{x:Type local:DiscsDataProvider}" /&gt; &lt;ObjectDataProvider x:Key="Discs" ObjectInstance="{StaticResource ResourceKey=DiscsDataProvider}" MethodName="GetDiscs" /&gt; &lt;Style x:Key="CellEditStyle" TargetType="{x:Type TextBox}"&gt; &lt;Setter Property="BorderThickness" Value="0"/&gt; &lt;Setter Property="Padding" Value="0"/&gt; &lt;Setter Property="Background" Value="Yellow"/&gt; &lt;Style.Triggers&gt; &lt;Trigger Property="Validation.HasError" Value="true"&gt; &lt;Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/&gt; &lt;/Trigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/Window.Resources&gt; </code></pre> <p>For the datagrid:</p> <pre><code>&lt;Grid Width="auto" Height="auto"&gt; &lt;DockPanel DataContext="{Binding Source={StaticResource ResourceKey=Discs}}"&gt; &lt;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"&gt; &lt;DataGrid.Columns&gt; &lt;DataGridTextColumn Binding="{Binding Path=ContainerID}" CanUserReorder="False" CanUserResize="True" CanUserSort="True" EditingElementStyle="{StaticResource CellEditStyle}" IsReadOnly="False" Header="Container" /&gt; &lt;DataGridTextColumn Binding="{Binding Path=ID}" CanUserReorder="False" CanUserResize="True" CanUserSort="True" EditingElementStyle="{StaticResource CellEditStyle}" IsReadOnly="True" Header="ID" /&gt; &lt;DataGridTextColumn Binding="{Binding Path=Title}" CanUserReorder="False" CanUserResize="True" CanUserSort="True" EditingElementStyle="{StaticResource CellEditStyle}" IsReadOnly="False" Header="Title" /&gt; &lt;DataGridTextColumn Binding="{Binding Path=SubTitle}" CanUserReorder="False" CanUserResize="True" CanUserSort="False" EditingElementStyle="{StaticResource CellEditStyle}" IsReadOnly="False" Header="Sub Title" /&gt; &lt;DataGridTextColumn Binding="{Binding Path=Type}" CanUserReorder="False" CanUserResize="True" CanUserSort="True" EditingElementStyle="{StaticResource CellEditStyle}" IsReadOnly="False" Header="Type" /&gt; &lt;DataGridTextColumn Binding="{Binding Path=Volume}" CanUserReorder="False" CanUserResize="True" CanUserSort="False" EditingElementStyle="{StaticResource CellEditStyle}" IsReadOnly="False" Header="Volume" /&gt; &lt;DataGridTextColumn Binding="{Binding Path=TotalDiscs}" CanUserReorder="False" CanUserResize="True" CanUserSort="False" EditingElementStyle="{StaticResource CellEditStyle}" IsReadOnly="False" Header="Total Discs" /&gt; &lt;/DataGrid.Columns&gt; &lt;/DataGrid&gt; &lt;/DockPanel&gt; </code></pre> <p>and C#:</p> <pre><code>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; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload