dcsimg
Results 1 to 2 of 2

Thread: Update DataGrid in WPF

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2018
    Posts
    47

    Update DataGrid in WPF

    Hey everyone,

    I have a linq to SQL query that populates a datagrid. I need the user to update some records in the datagrid. For example, I need them to type in the Actual savings on each record and save it back to the database. Am I able to do this directly in the datagrid with some property changes in the xaml?
    Code:
            <ListBox x:Name="SiteNameListBox" ItemsSource="{Binding}" 
                     DisplayMemberPath="SiteName" SelectedValuePath="IDSite" 
                     SelectionChanged="SiteNameListBox_SelectionChanged" Margin="-136,10,457,10" Grid.RowSpan="2"/>
            <DataGrid AutoGenerateColumns="False" ColumnWidth="*"  x:Name="dataGridView1" Margin="0,10,24,169" Grid.ColumnSpan="3" Grid.RowSpan="2">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding FirstName}" Header="FirstName"/>
                    <DataGridTextColumn Binding="{Binding LastName}" Header="LastName"/>
                    <DataGridTextColumn Binding="{Binding SiteName}" Header="SiteName"/>
                    <DataGridTextColumn Binding="{Binding ProjectName}" Header="ProjectName"/>
                    <DataGridTextColumn Binding="{Binding StartDate}" Header="StartDate"/>
                    <DataGridTextColumn Binding="{Binding EndDate}" Header="EndDate"/>
                    <DataGridTextColumn Binding="{Binding PlannedSavings}" Header="PlannedSavings"/>
                    <DataGridTextColumn Binding="{Binding ActualSavings}" Header="ActualSavings"/>
                </DataGrid.Columns>
            </DataGrid>
            <DataGrid HorizontalAlignment="Left" Height="120" Margin="10,61,0,0" Grid.Row="1" VerticalAlignment="Top" Width="714" Grid.ColumnSpan="3"/>
        </Grid>
    I'm including my query code as well for good measure:
    Code:
    private void loadgrid()
            {
                var context = new ProductivityEntities();
                var SelListBoxItem = (int)SiteNameListBox.SelectedValue;
                var q = from s in context.pt_Site
                        join t in context.pt_ProjectsSites on s.IDSite equals t.Site_id
                        join r in context.pt_Projects on t.Project_id equals r.IDProjects
                        join a in context.pt_ProjectSavings on r.IDProjects equals a.Project_id
                        join l in context.pt_Personnel on r.Personnel_id equals l.IDPersonnel
                        where s.IDSite == SelListBoxItem
                        select new { l.FirstName, l.LastName, s.SiteName, r.ProjectName, r.StartDate, r.EndDate, a.PlannedSavings, a.ActualSavings };
    
                dataGridView1.ItemsSource = q.ToList();
    
            }
    Attached Images Attached Images  

  2. #2

    Thread Starter
    Member
    Join Date
    Jun 2018
    Posts
    47

    Re: Update DataGrid in WPF

    So I was able to change my xaml to this:
    Code:
            <DataGrid AutoGenerateColumns="False" ColumnWidth="*"  x:Name="dataGridView1" Margin="0,10,24,169" Grid.ColumnSpan="3" Grid.RowSpan="2">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding FirstName}" Header="FirstName"/>
                    <DataGridTextColumn Binding="{Binding LastName}" Header="LastName"/>
                    <DataGridTextColumn Binding="{Binding SiteName}" Header="SiteName"/>
                    <DataGridTextColumn Binding="{Binding ProjectName}" Header="ProjectName"/>
                    <DataGridTextColumn Binding="{Binding StartDate}" Header="StartDate"/>
                    <DataGridTextColumn Binding="{Binding EndDate}" Header="EndDate"/>
                    <DataGridTextColumn Binding="{Binding PlannedSavings}" Header="PlannedSavings"/>
                    <DataGridTextColumn Binding="{Binding ActualSavings.savecommand, XPath=Value, Mode=TwoWay}" Header="ActualSavings"  IsReadOnly="False"/>
                </DataGrid.Columns>
            </DataGrid>
    and this allows me to type into the column I want to update, "ActualSavings", but I don't know how to save the changes to the database.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width