標簽:有用 初始化 code close ntc == wce row 文本
在桌面程序開發過程中我們常常使用DataGridView作為數據展示的表格,在表格中我們可能要對數據進行查找或者替換。
其實要實現這個查找替換的功能并不難,記錄下實現過程,不一定是最好的方式,但它有用!
先看demo下效果
public class Person
{
public int ID { get; set; }
public string Name { get; set; }
public string Sex { get; set; }
public int Age { get; set; }
}
這個窗體主要是用來控制查找和替換的文本,選擇范圍是當前列還是整個數據表格。
窗體中主要是查找替換文本的值,選中的查找范圍和是否能設置查找范圍變量;還包括4個事件,4個事件在GridDataWindow 中添加用于響應操作。
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; }
}
實例化一個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("未找到匹配項!");
}
打包了這個兩個窗體代碼:DataGridViewExcel.zip
WinForm使用DataGridView實現類似Excel表格的查找替換
標簽:有用 初始化 code close ntc == wce row 文本
原文地址:https://www.cnblogs.com/SunSpring/p/15040513.html