WPF中修改DataGrid单元格值并保存

编辑DataGrid中的单元格的内容然后保存是非常常用的功能。主要涉及到的方法就是DataGrid的CellEditEnding  和BeginningEdit 。其中BeginningEdit 是当单元格选中后,状态为可编辑状态时触发。CellEditEnding 是在单元格失去焦点后触发的事件。    对于编辑DataGrid中单元格内容的实现逻辑比较简单:

1.保存旧的单元格内容。
2.判断修改后的内容是否符合规范。
3.保存到数据库。
以下就是简单的实现逻辑,仅供参考:
前台代码:


<DataGrid Grid.Row="1" x:Name="dgData"
CellEditEnding="CellEditEnding"
BeginningEdit="BeginningEdit">
<!--需要完善,除了序号,基本完善-->
<DataGrid.Columns>

<DataGridTemplateColumn Header=" 序号" Width="50" MinWidth="10" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type DataGridRow}}, Path=Header}" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="10,0,0,0"></TextBlock>

</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

<DataGridTextColumn Binding="{Binding Name}" Width="*" Header="账套名称" IsReadOnly="False"/>

</DataGrid.Columns>
</DataGrid>

后台代码:

private void CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{


string newValue = (e.EditingElement as TextBox).Text;

//判断新名称和就名称是否一样
if (ZTName!=newValue)
{


DataAccess.IAccountDAL iaccount = DALResolver.Instance.Resolve<IAccountDAL>();

int count = iaccount.Count(newValue);

if (count>0)
{
MessageBox.Show("账套名称重复,请重新命名!", "警告", MessageBoxButton.OK);
(e.EditingElement as TextBox).Text = ZTName;
return;
}
else
{
int id = (this.dgData.SelectedItem as Founder.Model.Account.ModelAccountInfo).AccountID;
if (!iaccount.UpdateZTName(newValue,id))
{
MessageBox.Show("更新账套名称失败,请重试!","提示",MessageBoxButton.OK);
}
}
//判断是否重复

}

}

private string ZTName = string.Empty;
private void BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
{
ZTName = (this.dgData.SelectedItem as Founder.Model.Account.ModelAccountInfo).Name;
//MessageBox.Show(ZTName);
}

总结:该功能比较简单,主要就是熟悉DataGrid的熟悉和方法。在此,仅作记录。

转载于:https://www.cnblogs.com/yelanggu/p/10444632.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPFDataGrid可以通过绑定来实现单元格字体颜色的改变。在XAML,可以通过在DataGridCellStyle设置Trigger来实现,如下所示: ``` xml <DataGrid> <DataGrid.CellStyle> <Style TargetType="{x:Type DataGridCell}"> <Style.Triggers> <DataTrigger Binding="{Binding Path=YourBindingProperty}" Value="YourValue"> <Setter Property="Foreground" Value="Red"/> <!-- 设置字体颜色为红色 --> </DataTrigger> <Style.Triggers> </Style> </DataGrid.CellStyle> </DataGrid> ``` 其,YourBindingProperty是绑定到DataGrid的数据源的属性,当属性等于YourValue时,就会触发Trigger的设置,将字体颜色设置为红色。这里只是简单的例子,实际情况可能需要根据不同属性设置不同的颜色。 另外,也可以通过继承DataGrid来自定义单元格样式。在自定义的DataGrid,可以重写GetCellContainer方法来获取单元格,然后通过设置单元格的Foreground属性来改变字体颜色,如下所示: ``` csharp public class MyDataGrid : DataGrid { protected override System.Windows.DependencyObject GetContainerForCellOverride() { return new MyDataGridCell(); } } public class MyDataGridCell : DataGridCell { protected override void OnBindingChanged(System.Windows.DependencyPropertyChangedEventArgs e) { base.OnBindingChanged(e); Brush foreground; // 根据不同的属性设置不同的颜色 if(/* Your condition */) { foreground = Brushes.Red; } else if(/* Your condition */) { foreground = Brushes.Green; } else { foreground = Brushes.Black; } // 设置字体颜色 this.Foreground = foreground; } } ``` 通过自定义DataGridCell,并重写OnBindingChanged方法实现根据不同属性设置不同颜色,然后通过设置字体颜色来改变单元格字体颜色。 以上是两种实现DataGrid单元格字体颜色改变的方法,可以根据实际需要选择使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值