WPF DataGrid单元格值无法在运行时更改... [英] WPF DataGrid Cell value cannot change on runtime...
问题描述
在这里,我尝试使用dataGrid编辑/更新我的SQL Server数据库.
首先,我将值加载到datagrid.
然后尝试通过双击单元格来更改单元格的任何值.但是,在我移动光标之后,先前的值进入了该单元格.因此,相同的值保存到DB,而不是我想要保存的更改后的值.
删除代码可以正常工作.
据我所知,我认为C#代码可以正常工作.只有datagrid单元值无法更改的问题.
请提出建议,我是否需要更改任何datagrid属性值,或者这是一个不同的问题.
按下编辑按钮后...
数据库已使用先前的值成功更新,但未使用新值成功更新.
这是代码.... XAML
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>
代码... C#
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.
推荐答案
使用双向绑定模式{Binding Path = Temp06,Mode = Twoway}"
use twoway binding mode {Binding Path=Temp06, Mode=Twoway}"
我发现一个很好的解决方案
http://www.dotnetcurry.com/ShowArticle.aspx?ID=563
thanx gajenda解决方案.
I found a good solution from this
http://www.dotnetcurry.com/ShowArticle.aspx?ID=563
thanx gajenda for ur solution.
这篇关于WPF DataGrid单元格值无法在运行时更改...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!