Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF 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
0 OriginalGriff 490
1 Maciej Los 299
2 BillWoodruff 174
3 /\jmot 170
4 Suraj Sahoo | Coding Passion 150
0 OriginalGriff 8,484
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,159
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 3 Nov 2012
Copyright © CodeProject, 1999-2014
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