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的熟悉和方法。在此,仅作记录。
WPF中修改DataGrid单元格值并保存的更多相关文章
- QTableView中修改某个单元格或者行或者列内容颜色
QTableView的单元格内容实现还是继承了TableViewModel类的data(const QModelIndex &index, int role) const函数,那个设置颜色的问 ...
- Easyui的datagrid的行编辑器Editor中添加事件(修改某个单元格带出其他单元格的值)
项目中有个datagrid需要编辑行时,用到Editor的属性,那么如何添加一个事件 问题:同一个编辑行中的某个单元格值改变时,修改其他单元格的值 页面用到的datagrid <table id ...
- WPF学习笔记(8):DataGrid单元格数字为空时避免验证问题的解决
原文:WPF学习笔记(8):DataGrid单元格数字为空时避免验证问题的解决 如下图,在凭证编辑窗体中,有的单元格不需要数字,但如果录入数字后再删除,会触发数字验证,单元格显示红色框线,导致不能执行 ...
- Java 获取表格中某一个单元格的值
需求 搜索页面返回表格样搜索结果, 获取搜索结果中某个单元格的具体值. 以下图为例, 下表是搜索返回的结果, 第一行是各个列的名字, 其它是具体的返回值. 方法1: 根据用户输入的表头名来确定是第几列 ...
- jqGrid 修改单元格值或者替换图片及其他
var rowIds = jQuery("#list1").jqGrid('getDataIDs'); for (var k = 0; k < ...
- EasyUI Datagrid 单元格编辑
3:对于单元格的编辑 $('#Units').datagrid({ pageNumber: 1, //url: "@ViewBag.Domain/Paper/GetQuestionUnit& ...
- 基于1.3.3版本tooltip的datagrid单元格tip实现
基于1.3.3版本tooltip的datagrid单元格tip实现 2013年05月25日 ⁄ datagrid ⁄ 共 6122字 ⁄ 评论数 26 ⁄ 被围观 7,033 views+ 文章目录 ...
- 无法读取Excel中的数据单元格。有数据,但是读出来全是空值
C#读取Excel,取值为空的解决办法! C#读取Excel遇到无法读取的解决方法是什么呢?这样在C#读取Excel的过程中有很多问题,那么本文就向你介绍如何解决C#读取Excel遇到无法读取的解决方 ...
- jqgrid cellEdit为true的时候,默认选中单元格值的解决方案
jqgrid cellEdit为true的时候,点击单元格的时候,鼠标在单元格最前面闪. 这时候如果要修改数字内容,非常麻烦.要全选单元格内容,不然不好改. 点击单元格的时候,默认选中单元格值的解决方 ...
随机推荐
- Flask 系列之 HelloWorld
说明 操作系统:Windows 10 Python 版本:3.7x 虚拟环境管理器:virtualenv 代码编辑器:VS Code 实验目标 创建一个显示 Hello World 页面的网站. 环境 ...
- Fundebug累计处理1000万条错误事件!
摘要上线半年时间,Fundebug累计处理1000万条错误事件.10000000啊! 自从去年双11[正式上线](https://blog.fundebug.com/2016/11/11/fundeb ...
- C# Dev XtraReport 简单测试
using System; using System.Collections.Generic; using System.Linq; using System.Text; using DevExpre ...
- K8S flannel
kubernetes网络通信方式有: 容器间的通信 : pod内的容器通信 通过(lo)设备 Pod之间的通信 :pod IP <-----> pod IP ,K8S 要求所有的 pod ...
- CSS3动画属性:转换(transition)
W3C标准中对css3的transition这是样描述的:“css的transition允许css的属性值在一定的时间区间内平滑地过渡.这种效果可以在鼠标单击.获得焦点.被点击或对元素任何改变中触发, ...
- 洛谷P3245 [HNOI2016]大数(莫队)
题意 题目链接 Sol 莫队板子题.. 维护出每个位置开始的字符串\(mod P\)的结果,记为\(S_i\) 两个位置\(l, r\)满足条件当且仅当\(S_l - S_r = 0\),也就是\(S ...
- oppor9手机怎么录制屏幕视频
我们已经进入互联网时代,每个人都寸步不离手机.电脑等电子产品,看到美丽好看的视频总想记录下来,毕竟看到喜欢的视频还真不太容易,所以问题来了,oppor9手机怎么录制屏幕视频呢?安卓手机上怎么录制屏幕视 ...
- 使用SQL查看表字段和字段说明
MySql: show full columns from tableName; Sql server: SELECT A.name AS table_name, B.name AS column_n ...
- JavaScript常用函数
JavaScript常用函数 常规函数 数组函数 日期函数 数学函数 字符串函数 常规函数 (1)alert函数:显示一个警告对话框,包括一个OK按钮.(alert("输入错误") ...
- Vue之resource请求数据
导入resource文件 <script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.min.js& ...