Can we take a WPF DataGrid directly into a DataSet?

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


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



<Window x:Class="CDF.WorkDays"
    Title="WorkDays" Height="411" Width="620" xmlns:dg="" Loaded="Window_Loaded">
            <ColumnDefinition Width="603*" />
            <ColumnDefinition Width="27*" />
        <dg:DataGrid x:Name="WorkDaysDataGrid"   AlternatingRowBackground="LightGray"  AutoGenerateColumns="False"  CanUserSortColumns="False" Margin="0,0,26,45" Grid.ColumnSpan="2">

                <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" />

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

Code Behind



    public partial class WorkDays : Window
        public WorkDays()

        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