编辑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的熟悉和方法。在此,仅作记录。

WPF中修改DataGrid单元格值并保存的更多相关文章

  1. QTableView中修改某个单元格或者行或者列内容颜色

    QTableView的单元格内容实现还是继承了TableViewModel类的data(const QModelIndex &index, int role) const函数,那个设置颜色的问 ...

  2. Easyui的datagrid的行编辑器Editor中添加事件(修改某个单元格带出其他单元格的值)

    项目中有个datagrid需要编辑行时,用到Editor的属性,那么如何添加一个事件 问题:同一个编辑行中的某个单元格值改变时,修改其他单元格的值 页面用到的datagrid <table id ...

  3. WPF学习笔记(8):DataGrid单元格数字为空时避免验证问题的解决

    原文:WPF学习笔记(8):DataGrid单元格数字为空时避免验证问题的解决 如下图,在凭证编辑窗体中,有的单元格不需要数字,但如果录入数字后再删除,会触发数字验证,单元格显示红色框线,导致不能执行 ...

  4. Java 获取表格中某一个单元格的值

    需求 搜索页面返回表格样搜索结果, 获取搜索结果中某个单元格的具体值. 以下图为例, 下表是搜索返回的结果, 第一行是各个列的名字, 其它是具体的返回值. 方法1: 根据用户输入的表头名来确定是第几列 ...

  5. jqGrid 修改单元格值或者替换图片及其他

     var rowIds = jQuery("#list1").jqGrid('getDataIDs');                for (var k = 0; k < ...

  6. EasyUI Datagrid 单元格编辑

    3:对于单元格的编辑 $('#Units').datagrid({ pageNumber: 1, //url: "@ViewBag.Domain/Paper/GetQuestionUnit& ...

  7. 基于1.3.3版本tooltip的datagrid单元格tip实现

    基于1.3.3版本tooltip的datagrid单元格tip实现 2013年05月25日 ⁄ datagrid ⁄ 共 6122字 ⁄ 评论数 26 ⁄ 被围观 7,033 views+ 文章目录 ...

  8. 无法读取Excel中的数据单元格。有数据,但是读出来全是空值

    C#读取Excel,取值为空的解决办法! C#读取Excel遇到无法读取的解决方法是什么呢?这样在C#读取Excel的过程中有很多问题,那么本文就向你介绍如何解决C#读取Excel遇到无法读取的解决方 ...

  9. jqgrid cellEdit为true的时候,默认选中单元格值的解决方案

    jqgrid cellEdit为true的时候,点击单元格的时候,鼠标在单元格最前面闪. 这时候如果要修改数字内容,非常麻烦.要全选单元格内容,不然不好改. 点击单元格的时候,默认选中单元格值的解决方 ...

随机推荐

  1. QueryRunner使用之可变条件的处理

    在三层架构的Dao层中,需要通过不确定的条件,从数据库查询结果. 可以利用List集合作为容器将条件存储起来. 实际开发中的代码: public List<Hotel> searchByF ...

  2. 给大家带来一些 horm的一些知识!

    1. form action 提交地址     https://www.baidu.com merhod 提交 方式post get get是在http的url上提交不加密 post加密在http 中 ...

  3. Advanced redirection features

    here are three types of I/O, which each have their own identifier, called a file descriptor: standar ...

  4. 提高 JavaScript 开发效率的高级 VSCode 扩展!

    原文:提高 JavaScript 开发效率的高级 VSCode 扩展! 作者:前端小智 Fundebug经授权转载,版权归原作者所有. Quokka.js Quokka.js 是一个用于 JavaSc ...

  5. iOS----------开发中常用的宏有那些

    OC对象判断是否为空? 字符串是否为空 #define kStringIsEmpty(str) ([str isKindOfClass:[NSNull class]] || str == nil || ...

  6. Python date,datetime,time等相关操作总结

    date,datetime,time等相关操作总结   by:授客 QQ:1033553122 测试环境: Python版本:Python 3.3.2 代码实践: __author__ = '授客' ...

  7. Android 9.0新特性

    1.全面屏支持,Android P加入了对刘海屏的支持,谷歌称之为凹口屏幕(display with a cutout).借助最新的提供的DisplayCutout类,开发者可以找到非功能区域的位置和 ...

  8. 用户登录三次机会(PYTHON)

    usename=shabi password=123456 i=3 while i > 0: zh = input("请输入你的用户名:") i - = 1 if zh == ...

  9. Django 配置MySQL数据库

    在settings.py中配置 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', # 数据库引擎 'NAME': 'my ...

  10. [20190101]块内重整.txt

    [20190101]块内重整.txt --//我不知道用什么术语表达这样的情况,我仅仅一次开会对方这么讲,我现在也照用这个术语.--//当dml插入数据到数据块时,预留一定的空间(pctfree的百分 ...