Can we take a WPF DataGrid directly into a DataSet?

Nov 21, 2010 at 8:07 AM
Edited Nov 21, 2010 at 8:13 AM

Hi,

I have the following form which is bound to a DataSet. It has 2 columns which is editable and I need to persist these two column values in a database. I have been trying many things to get the modified DataSet but upto now I could not do anything.

I would like to know what I'm doing wrong or whether its possible to do what I'm doing.

The XAML and the Code beind is attached below

XAML

 

<Window x:Class="CDF.WorkDays"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WorkDays" Height="411" Width="620" xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit" Loaded="Window_Loaded">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="603*" />
            <ColumnDefinition Width="27*" />
        </Grid.ColumnDefinitions>
        <dg:DataGrid x:Name="WorkDaysDataGrid"   AlternatingRowBackground="LightGray"  AutoGenerateColumns="False"  CanUserSortColumns="False" Margin="0,0,26,45" Grid.ColumnSpan="2">
            <dg:DataGrid.Columns>

                <dg:DataGridTextColumn  Binding="{Binding Path=AgentID}" Header="Agent ID" IsReadOnly="True" FontWeight="bold"/>
                <dg:DataGridTextColumn Binding="{Binding Path=FirstName}" Header="First Name"  IsReadOnly="True" FontWeight="bold"/>
                <dg:DataGridTextColumn Binding="{Binding Path=MiddleNames}" Header="Middle Names"  IsReadOnly="True" FontWeight="bold"/>
                <dg:DataGridTextColumn Binding="{Binding Path=LastName}" Header="Last Name" IsReadOnly="True"/>
                <dg:DataGridTextColumn Binding="{Binding Path=DaysWorked}" Header="Number of Days" />
                <dg:DataGridCheckBoxColumn Binding="{Binding Path=Verified}" Header="Approval Status" />



            </dg:DataGrid.Columns>
        </dg:DataGrid>
        <Button Height="23" Margin="0,0,26,12" Name="button1" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="75" Grid.ColumnSpan="2" Click="button1_Click">Button</Button>
    </Grid>
</Window>

Code Behind

 

 

    public partial class WorkDays : Window
    {
        public WorkDays()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            using (CDFDataContext cdf = new CDFDataContext())
            {
                WorkDaysDataGrid.DataContext = (new DataAccessLayer()).getAgentByStation(121).Tables[0];



                Binding bind = new Binding();

                WorkDaysDataGrid.DataContext = (new DataAccessLayer()).getAgentByStation(121).Tables[0];

                WorkDaysDataGrid.SetBinding(DataGrid.ItemsSourceProperty, bind);

            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            DataSet ds = new DataSet();

            //I need to take the values in the DataGrid to the DataSet ds
        }
}