This is the 24th day of my participation in the August Text Challenge.More challenges in August

In desktop development, we often use the DataGridView as a table for displaying data, in which we may need to find or replace data. It’s not that hard to implement this find and replace feature. Documenting the process is not necessarily the best way, but it works! First look at the demo effect

1. Data presentation

  • Create a WinForm GridDataWindow, place the menu and DataGridView control, and add four columns to display the information.

  • Create a Person class to display the data
public class Person { public int ID { get; set; } public string Name { get; set; } public string Sex { get; set; } public int Age { get; set; }}Copy the code
  • Initialize the display data in the form Load event

2. Find the replacement form

  • Create a WinForm DataToolsWindow

This form is mainly used to control the text to be found and replaced by the current column or the entire table of data. The form is mainly to find the value of the replacement text, the selected search range and whether the search range variable can be set; It also includes four events that are added to the GridDataWindow for the response operation.

  • LookUpHandler: Click Find to find the table cells in turn based on the selected range and value.
  • ReplaceHandler: Replaces the text, finds the table cells in turn based on the selected range and value, and replaces them if found.
  • ReplaceAllHandler: Replace all, find all table cells based on the selected range and value, find and replace all.
  • WindownClosedHandler: The window closes. The main window is notified when the lookup window closes and does the required logic.
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; }}Copy the code

3. How do I find replacements

Instantiate a DataToolsWindow and register the event. The key is how to find, because substitution is the same as finding, as long as you find the substitution.

  • Find the next one

The general idea is to mark the current cell [selected], first look down with the current cell as the dividing line, and judge whether the user selects the current column or the entire data table during the search. If it is the current column, only need to look up the current column by row. If the search is for the entire table, the search is for every column of the entire row. If the search is for the selected row, the search is for the column in front of the current column (the following columns are traversed in the search down). If the search is not for the selected row, the search is for all columns of the entire row starting from the first column. Similarly, the idea of looking down comes up.

private void ToolsWindow_LookUpHandler(object sender, EventArgs e) { int currentRowIndex = dgvPeople.CurrentCell.RowIndex; int currentColumnIndex = dgvPeople.CurrentCell.ColumnIndex; Foreach (DataGridViewRow row in dgvpeople.rows) {// Look down if (row.index >= currentRowIndex) {if (toolswindow.alllookup) Foreach (DataGridViewCell cell in row.cells) {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 {// otherwise find foreach (DataGridViewCell cell in row.cells) {if (cell.value! = null && cell.Value.ToString().Contains(toolsWindow.LookupContent)) { cell.Selected = true; dgvPeople.CurrentCell = cell; return; }}}} else {// field search does not search current because search next 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) {// Look up if (row.index <= currentRowIndex) {if If (currentRowIndex == row.index) {foreach (DataGridViewCell cell in) {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 {// otherwise find foreach (DataGridViewCell cell in row.cells) {if (cell.value! = null && cell.Value.ToString().Contains(toolsWindow.LookupContent)) { cell.Selected = true; dgvPeople.CurrentCell = cell; return; }}}} else {// field search does not search current because search next 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 (" No match found!" ); }Copy the code
  • Replace the next one

Replace is relatively simple, first if the selected column is the value of the search directly replace, and then replace according to the search idea to find the next replacement line, the code is basically the same there is no need to put garbage code.

  • All replacement

Replace all instead of having to find the next one to display the search process, just iterate through all the cells to replace it and select it for the user to view.

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 (" No match found!" ); }Copy the code

4. Source files

Package the two form codes: DataGridViewExcel. Zip