Click here to Skip to main content
12,072,392 members (53,781 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# WPF VS2010 wpftoolkit
Here I am trying to edit / update my SQL Server DB using dataGrid.
1st I load the values to datagrid.
Then Trying to change any value of a cell by double clicking cell. But, after I move the cursor, previous value comes to the cell. So same value saves to DB, not the changed value I wanted to save.
The deleting code works fine.
As my knowledge I think C# code is working fine. Only the problem that datagrid cell value cannot change.
Please advice that do I need to change any datagrid property value or is this a different issue.

After pressing Edit button...

DB updated successfully with previous value, but not with new value.
Here is code....XAML

<Grid>
        <TextBox Height="23" HorizontalAlignment="Left" Margin="12,21,0,0" Name="txtContNo" VerticalAlignment="Top" Width="120" />
        <Button Content="Load" Height="23" HorizontalAlignment="Left" Margin="157,21,0,0" Name="btnLoad" VerticalAlignment="Top" Width="75" Click="btnLoad_Click" />
        <DataGrid AutoGenerateColumns="False" Height="374" HorizontalAlignment="Left" Margin="12,61,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="666" AllowDrop="True" UseLayoutRounding="True" BorderThickness="3" AlternationCount="2" SelectionMode="Single" IsEnabled="True" IsSynchronizedWithCurrentItem="True" SelectionUnit="FullRow" IsReadOnly="False" IsManipulationEnabled="True">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Path=Date1, Mode=OneTime}"   Width="70" Header="DATE" FontStyle="Normal" FontWeight="Normal" />
                <DataGridTextColumn Binding="{Binding Path=Temp00, Mode=OneTime}"  Width="SizeToHeader" Header="TEMP0000" />
                <DataGridTextColumn Binding="{Binding Path=Temp06, Mode=OneTime}"  Width="SizeToHeader" Header="TEMP0600" />
                <DataGridTextColumn Binding="{Binding Path=Temp12, Mode=OneTime}"  Width="SizeToHeader" Header="TEMP1200" />
                <DataGridTextColumn Binding="{Binding Path=Temp18, Mode=OneTime}"  Width="SizeToHeader" Header="TEMP1800" />
                <DataGridTextColumn Binding="{Binding Path=DLane, Mode=OneTime}"  Width="50" Header="LANE" />
                <DataGridTextColumn Binding="{Binding Path=DVentilation, Mode=OneTime}"  Width="SizeToHeader" Header="VENTILATION" />
                <DataGridTextColumn Binding="{Binding Path=DRemarks, Mode=OneTime}"  Width="SizeToHeader" Header="REMARKS" />
                <DataGridTemplateColumn Header="Edit Row">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Content="Edit" Click="EditButton_Click" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Delete Row">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Content="Delete" Click="DeleteButton_Click" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
            <DataGrid.AlternatingRowBackground>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#7FFF00FF" Offset="0" />
                    <GradientStop Color="White" Offset="1" />
                </LinearGradientBrush>
            </DataGrid.AlternatingRowBackground>
        </DataGrid>
        <Label Content="Container Number" Height="28" HorizontalAlignment="Left" Margin="9,0,0,0" Name="label1" VerticalAlignment="Top" />
    </Grid>

Code...C#

namespace comoco_wpf
{
    /// <summary>
    /// Interaction logic for WindowEdit1.xaml
    /// </summary>
    public partial class WindowEdit1 : Window
    {
        public WindowEdit1()
        {
            InitializeComponent();
 
        }
 
        private void btnLoad_Click(object sender, RoutedEventArgs e)
        {
            LoadDates();
        }
 
        private void LoadDates()
        {
            comocoLTSDataContext dc = new comocoLTSDataContext();
            var history = from d in dc.DCdates
                          where d.DCcontainer.ContainerNo == txtContNo.Text
                          select d;
            dataGrid1.ItemsSource = history;
        }
 
        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            comocoLTSDataContext dc = new comocoLTSDataContext();
            DCdate dateRow = dataGrid1.SelectedItem as DCdate;
            DCdate date = (from d in dc.DCdates
                           where d.GKId == dateRow.GKId & d.DId == dateRow.DId
                           select d).Single();
            dc.DCdates.DeleteOnSubmit(date);
            dc.SubmitChanges();
            MessageBox.Show("Row deleted successfully");
            LoadDates();
        }
 
        private void EditButton_Click(object sender, RoutedEventArgs e)
        {
 
            try
            {
                comocoLTSDataContext dc = new comocoLTSDataContext();
                DCdate dateRow = dataGrid1.SelectedValue as DCdate;
                //int m = dateRow.GKId;
                var date = (from d in dc.DCdates
                            where d.DId == dateRow.DId & d.DCcontainer.ContainerNo == txtContNo.Text
                            select d).Single();
                date.Date1 = dateRow.Date1;
                date.Temp00 = dateRow.Temp00;
                date.Temp06 = dateRow.Temp06;
                date.Temp12 = dateRow.Temp12;
                date.Temp18 = dateRow.Temp18;
                date.DLane = dateRow.DLane;
                date.DVentilation = dateRow.DVentilation;
                date.DRemarks = dateRow.DRemarks;
                dc.SubmitChanges();
                MessageBox.Show("Row Updated Successfully");
                LoadDates();
 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }}}}

Please advice as soon as possible.
Thanks in advanced.
Posted 30-Oct-12 19:25pm
kasun.s310
Edited 30-Oct-12 19:30pm
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

use twoway binding mode {Binding Path=Temp06, Mode=Twoway}"
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

I found a good solution from this
http://www.dotnetcurry.com/ShowArticle.aspx?ID=563


thanx gajenda for ur solution.
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web04 | 2.8.160208.1 | Last Updated 3 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100