WinForm使用DataGridView實(shí)現(xiàn)類似Excel表格的查找替換功能
在桌面程序開發(fā)過程中我們常常使用DataGridView作為數(shù)據(jù)展示的表格,在表格中我們可能要對數(shù)據(jù)進(jìn)行查找或者替換。
其實(shí)要實(shí)現(xiàn)這個查找替換的功能并不難,記錄下實(shí)現(xiàn)過程,不一定是最好的方式,但它有用!
先看demo下效果

1、數(shù)據(jù)展示建一個WinForm窗體 GridDataWindow ,放上菜單和DataGridView控件,添加4列用來顯示信息。

創(chuàng)建一個Person類用于顯示數(shù)據(jù)
public class Person
{
public int ID { get; set; }
public string Name { get; set; }
public string Sex { get; set; }
public int Age { get; set; }
}
在窗體Load事件里面初始化顯示數(shù)據(jù)

2、查找替換窗體建一個WinForm窗體 DataToolsWindow

這個窗體主要是用來控制查找和替換的文本,選擇范圍是當(dāng)前列還是整個數(shù)據(jù)表格。
窗體中主要是查找替換文本的值,選中的查找范圍和是否能設(shè)置查找范圍變量;還包括4個事件,4個事件在GridDataWindow 中添加用于響應(yīng)操作。
- LookUpHandler:點(diǎn)擊查找,根據(jù)選擇的范圍和值依次查找表格單元格。
- ReplaceHandler:替換文本,根據(jù)選擇的范圍和值依次查找表格單元格,如果查找到則替換。
- ReplaceAllHandler:全部替換,根據(jù)選擇的范圍和值依次查找所有表格單元格,查找到并全部替換。WindownClosedHandler:窗體關(guān)閉,當(dāng)查找窗體關(guān)閉后主窗體得到通知并做些需要的邏輯。
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、如何查找替換

實(shí)例化一個DataToolsWindow后對事件進(jìn)行注冊。重點(diǎn)是如何查找,因?yàn)樘鎿Q和查找一樣,只要查找到了替換就行了。
- 查找下一個
大概的思路就是按照【選定】的當(dāng)前單元格為標(biāo)記,首先以當(dāng)前單元格為分界線向下查找,在查找的過程中判斷用戶選擇的是當(dāng)前列還是整個數(shù)據(jù)表,如果是當(dāng)前列只需要按行查找當(dāng)前列就行了。
如果是整個數(shù)據(jù)表查找則需要整行的每列都查找,如果查找到選中行查找的列就是找當(dāng)前列前面的列(后面的列會在向下查找中遍歷到),如果不是選中行則整行從第一列開始全部列查找。
同理,向下查找的思路也就出來了。
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)
{
//如果是當(dāng)前選中行 則查找后面的列
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
{
//字段查找不查找當(dāng)前 因?yàn)槭遣檎蚁乱粋€
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)
{
//如果是當(dāng)前選中行 只查找前面的列
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
{
//字段查找不查找當(dāng)前 因?yàn)槭遣檎蚁乱粋€
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("未找到匹配項!");
}
- 替換下一個
替換就比較簡單了,首先如果選中列就是查找的值則直接替換,然后再替換則按照查找的思路查找到下一個后替換就行了,代碼基本一樣就沒必要放垃圾代碼了。
- 全部替換
全部替換就不用查找下一個要顯示查找過程那么麻煩了,直接遍歷所有單元格進(jìn)行替換并選中供用戶查看就行了。
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
到此這篇關(guān)于WinForm使用DataGridView實(shí)現(xiàn)類似Excel表格的查找替換的文章就介紹到這了,更多相關(guān)DataGridView實(shí)現(xiàn)表格的查找替換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C#開發(fā)WinForm根據(jù)條件改變DataGridView行顏色
- C#開發(fā)WinForm之DataGridView開發(fā)詳解
- Winform讓DataGridView左側(cè)顯示圖片
- Winform在DataGridView中顯示圖片
- WinForm中DataGridView折疊控件【超好看】
- winform用datagridview制作課程表實(shí)例
- WinForm中DataGridView添加,刪除,修改操作具體方法
- WinForm DataGridView控件隔行變色的小例子
- C#開發(fā)WinForm清空DataGridView控件綁定的數(shù)據(jù)
相關(guān)文章
C#獲取系統(tǒng)當(dāng)前日期和時間的示例詳解
這篇文章主要為大家詳細(xì)介紹了C#如何使用DateTime的Now靜態(tài)屬性動態(tài)獲得系統(tǒng)當(dāng)前日期和時間,文中的示例代碼講解詳細(xì),有需要的小伙伴可以參考一下2024-01-01
關(guān)于C#.net winform程序驗(yàn)證moss的集成身份認(rèn)證實(shí)例
因?yàn)榫W(wǎng)站使用的是windows集成認(rèn)證,所以遇到了權(quán)限問題,需要輸入密碼。使操作和用戶體驗(yàn)非常不方便,研究了好久沒有找到好的方法,最后終于讓我踏破鐵鞋總結(jié)出了下面的方法2013-03-03
C#實(shí)現(xiàn)一個簡單實(shí)用的TXT文本操作及日志框架詳解
這篇文章主要給大家介紹了關(guān)于利用C#如何實(shí)現(xiàn)一個簡單實(shí)用的TXT文本操作及日志框架的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們一起來看看吧2018-07-07

