WinForm使用DataGridView实现类似Excel表格的查找替换
在桌面程序开发过程中我们常常使用DataGridView作为数据展示的表格,在表格中我们可能要对数据进行查找或者替换。
其实要实现这个查找替换的功能并不难,记录下实现过程,不一定是最好的方式,但它有用!
先看demo下效果

1、数据展示
- 建一个WinForm窗体 GridDataWindow ,放上菜单和DataGridView控件,添加4列用来显示信息。

- 创建一个Person类用于显示数据
public class Person
{
public int ID { get; set; }
public string Name { get; set; }
public string Sex { get; set; }
public int Age { get; set; }
}
- 在窗体Load事件里面初始化显示数据

2、查找替换窗体
- 建一个WinForm窗体 DataToolsWindow

这个窗体主要是用来控制查找和替换的文本,选择范围是当前列还是整个数据表格。
窗体中主要是查找替换文本的值,选中的查找范围和是否能设置查找范围变量;还包括4个事件,4个事件在GridDataWindow 中添加用于响应操作。
- LookUpHandler:点击查找,根据选择的范围和值依次查找表格单元格。
- ReplaceHandler:替换文本,根据选择的范围和值依次查找表格单元格,如果查找到则替换。
- ReplaceAllHandler:全部替换,根据选择的范围和值依次查找所有表格单元格,查找到并全部替换。
- WindownClosedHandler:窗体关闭,当查找窗体关闭后主窗体得到通知并做些需要的逻辑。
public event EventHandler LookUpHandler;
public event EventHandler ReplaceHandler;
public event EventHandler ReplaceAllHandler;
public event EventHandler WindownClosedHandler;
public bool AllLookup
{
get
{
if (cbRange.SelectedIndex == 1)
return true;
else
return false;
}
set
{
if (value)
{
cbRange.SelectedIndex = 1;
}
else
{
cbRange.SelectedIndex = 0;
}
}
}
public bool CanSetRang
{
set
{
btnLookup.Enabled = false;
btnReplace.Enabled = false;
btnAllReplace.Enabled = false;
}
}
public string LookupContent
{
get { return txtLookup.Text; }
set { txtLookup.Text = value; }
}
public string ReplaceContent
{
get { return txtReplace.Text; }
}
3、如何查找替换

实例化一个DataToolsWindow后对事件进行注册。重点是如何查找,因为替换和查找一样,只要查找到了替换就行了。
- 查找下一个
大概的思路就是按照【选定】的当前单元格为标记,首先以当前单元格为分界线向下查找,在查找的过程中判断用户选择的是当前列还是整个数据表,如果是当前列只需要按行查找当前列就行了。
如果是整个数据表查找则需要整行的每列都查找,如果查找到选中行查找的列就是找当前列前面的列(后面的列会在向下查找中遍历到),如果不是选中行则整行从第一列开始全部列查找。
同理,向下查找的思路也就出来了。
private void ToolsWindow_LookUpHandler(object sender, EventArgs e)
{
int currentRowIndex = dgvPeople.CurrentCell.RowIndex;
int currentColumnIndex = dgvPeople.CurrentCell.ColumnIndex;
foreach (DataGridViewRow row in dgvPeople.Rows)
{
//向下查找
if (row.Index >= currentRowIndex)
{
if (toolsWindow.AllLookup)
{
//如果是当前选中行 则查找后面的列
if (currentRowIndex == row.Index)
{
foreach (DataGridViewCell cell in row.Cells)
{
if (cell.ColumnIndex > currentColumnIndex)
{
if (cell.Value != null && cell.Value.ToString().Contains(toolsWindow.LookupContent))
{
cell.Selected = true;
dgvPeople.CurrentCell = cell;
return;
}
}
}
}
else
{ //否则从第一列开始查找
foreach (DataGridViewCell cell in row.Cells)
{
if (cell.Value != null && cell.Value.ToString().Contains(toolsWindow.LookupContent))
{
cell.Selected = true;
dgvPeople.CurrentCell = cell;
return;
}
}
}
}
else
{
//字段查找不查找当前 因为是查找下一个
if (row.Index == currentRowIndex)
continue;
if (row.Cells[currentColumnIndex].Value != null && row.Cells[currentColumnIndex].Value.ToString().Contains(toolsWindow.LookupContent))
{
row.Cells[currentColumnIndex].Selected = true;
dgvPeople.CurrentCell = row.Cells[currentColumnIndex];
return;
}
}
}
}
foreach (DataGridViewRow row in dgvPeople.Rows)
{
//向上查找
if (row.Index <= currentRowIndex)
{
if (toolsWindow.AllLookup)
{
//如果是当前选中行 只查找前面的列
if (currentRowIndex == row.Index)
{
foreach (DataGridViewCell cell in row.Cells)
{
if (cell.ColumnIndex < currentColumnIndex)
{
if (cell.Value != null && cell.Value.ToString().Contains(toolsWindow.LookupContent))
{
cell.Selected = true;
dgvPeople.CurrentCell = cell;
return;
}
}
}
}
else
{ //否则从第一列开始查找
foreach (DataGridViewCell cell in row.Cells)
{
if (cell.Value != null && cell.Value.ToString().Contains(toolsWindow.LookupContent))
{
cell.Selected = true;
dgvPeople.CurrentCell = cell;
return;
}
}
}
}
else
{
//字段查找不查找当前 因为是查找下一个
if (row.Index == currentRowIndex)
continue;
if (row.Cells[currentColumnIndex].Value != null && row.Cells[currentColumnIndex].Value.ToString().Contains(toolsWindow.LookupContent))
{
row.Cells[currentColumnIndex].Selected = true;
dgvPeople.CurrentCell = row.Cells[currentColumnIndex];
return;
}
}
}
}
MessageBox.Show("未找到匹配项!");
}
- 替换下一个
替换就比较简单了,首先如果选中列就是查找的值则直接替换,然后再替换则按照查找的思路查找到下一个后替换就行了,代码基本一样就没必要放垃圾代码了。
- 全部替换
全部替换就不用查找下一个要显示查找过程那么麻烦了,直接遍历所有单元格进行替换并选中供用户查看就行了。
private void ToolsWindow_ReplaceAllHandler(object sender, EventArgs e)
{
bool IsReplace = false;
int currentColumnIndex = dgvPeople.CurrentCell.ColumnIndex;
foreach (DataGridViewRow row in dgvPeople.Rows)
{
if (toolsWindow.AllLookup)
{
foreach (DataGridViewCell cell in row.Cells)
{
if (cell.ColumnIndex != 0 && cell.Value != null && cell.Value.ToString().Contains(toolsWindow.LookupContent))
{
cell.Selected = true;
cell.Value = cell.Value.ToString().Replace(toolsWindow.LookupContent, toolsWindow.ReplaceContent);
IsReplace = true;
}
}
}
else
{
if (row.Cells[currentColumnIndex].Value != null && row.Cells[currentColumnIndex].Value.ToString().Contains(toolsWindow.LookupContent))
{
row.Cells[currentColumnIndex].Selected = true;
row.Cells[currentColumnIndex].Value = row.Cells[currentColumnIndex].Value.ToString().Replace(toolsWindow.LookupContent, toolsWindow.ReplaceContent);
IsReplace = true;
}
}
}
if (!IsReplace)
MessageBox.Show("未找到匹配项!");
}
4、源文件
打包了这个两个窗体代码:DataGridViewExcel.zip
WinForm使用DataGridView实现类似Excel表格的查找替换的更多相关文章
- winform DataGridView 导出到Excel表格 分类: WinForm 2014-07-04 10:48 177人阅读 评论(0) 收藏
public bool ExportDataGridview(DataGridView gridView) { if (gridView.Rows.Count ...
- Winform 中 dataGridView 导出到Excel中的方法总结
最近,在做CS端数据导出到Excel中时网上找了很多代码感觉都不是自己想要的,通过自己的整理归纳得到一个比较通用的方法,就给大家分享一下: 该方法需要用到两个参数(即对象),一个 DataGridV ...
- WinForm中DataGridView导出为Excel(快速版)
public static void ExportExcel(DataGridView myDGV, string fileName) { string saveFileName = fileName ...
- .Net语言 APP开发平台——Smobiler学习日志:如何在手机上显示类似EXCEL表格
最前面的话:Smobiler是一个在VS环境中使用.Net语言来开发APP的开发平台,也许比Xamarin更方便 样式一 一.目标样式 我们要实现上图中的效果,需要如下的操作: 1.从工具栏上的&qu ...
- Word中类似正则匹配的查找替换通配符的使用详解
一.Word查找栏代码&通配符一览表 序号 清除使用通配符复选框 勾选使用通配符复选框 特殊字符 代码 特殊字符 代码or通配符 1 任意单个字符 ^? 任意单个字符 ? 2 任意数字 ^# ...
- 详解如何利用FarPoint Spread表格控件来构造Winform的Excel表格界面输入
我们先来简单了解一下WinForm和FarPoint,WinForm是·Net开发平台中对Windows Form的一种称谓.而FarPoint是一款模拟EXCEL的控件.它可以根据用户的要求实现很大 ...
- C#读取Excel表格数据到DataGridView中和导出DataGridView中的数据到Excel
其实想在datagridview中显示excel表格中的数据跟读取数据库中的数据没什么差别,只不过是创建数据库连接的时候连接字段稍有差别. private void btnShow_Click(obj ...
- NPOI导出excel表格应用
最近接到一个需求,在原有系统上做二次开发 ,要求导出DataGridView数据到Excel表格中.要求如下: 兼容所有excel版本: 导出后excel各列的样式,字段类型不变. 成型如下:
- 如何在web中实现类似excel的表格控件
Execl功能非常强大,内置的很多函数或公式可以大大提高对数据的加工处理能力.那么在web中有没有类似的控件呢?经过一番搜寻,发现handsontable具备了基本的excel功能支持公式,同时能对数 ...
随机推荐
- BTC芯片介绍
BTC芯片介绍 Innosilicon宣布全球第一和最佳的28nm比特币ASIC和参考矿机 A1Craft(也称为A1)是2013年世界上最好的BTC ASIC,这是比特币区块哈希算法的易于使用,定制 ...
- Yolov4性能分析(下)
Yolov4性能分析(下) 六. 权重更新 "darknet/src/detector.c"--train_detector()函数中: ...... /* 开始训练网络 */ f ...
- Spring Cloud系列(五):服务网关Zuul
在前面的篇章都是一个服务消费者去调用一个服务提供者,但事实上我们的系统基本不会那么简单,如果真的是那么简单的业务架构我们也没必要用Spring Cloud,直接部署一个Spring Boot应用就够了 ...
- IDEA骚技巧
1. var 声明 2. null 判空 3. notnull 判非空 4. nn 判非空 5. for 遍历 6. fori 带索引的遍历 7. not 取反 8. if 条件判断 9. cast ...
- centos 7 iotop 安装
安装指令:yum -y install iotop 指定查看aubunt 用户的读写状态:iotop -u aubunt -P -k -t 允许在非交互模式下每隔3秒刷新一次,只刷新6次:iotop ...
- 《吃透MQ系列》之扒开Kafka的神秘面纱
大家好,这是<吃透 MQ 系列>的第二弹,有些珊珊来迟,后台被好几个读者催更了,实属抱歉! 这篇文章拖更了好几周,起初的想法是:围绕每一个具体的消息中间件,不仅要写透,而且要控制好篇幅,写 ...
- 题解 P3233 [HNOI2014]世界树
题目传送门 解题思路 正解当然是虚树了. 首先对于原树以及虚树各开一个结构体存边,这个不用多说. 然后我们先 DFS 一遍,求出各个节点的时间戳,子树大小,深度以及父亲节点,并初始化倍增 LCA . ...
- DOS命令行(4)——Windows系统的配置与管理(上)
sfc --运行系统文件检查器(需要以管理员身份运行命令提示符) 命令格式:SFC [/SCANNOW] [/VERIFYONLY] [/SCANFILE=<file>] [/VERIFY ...
- CMake 两种变量原理
目录 [TOC] 1.两种变量的定义参考 2.两种变量的作用域原理及使用 1.Normal Variables (1).包含 add_subdirectory().function().(本质是值拷贝 ...
- Pandas高级教程之:plot画图详解
目录 简介 基础画图 其他图像 bar stacked bar barh Histograms box Area Scatter Hexagonal bin Pie 在画图中处理NaN数据 其他作图工 ...