When developing a WinForm form program, we want to add a dialog box to search for the DataGridView data, similar to the "search and replace" dialog box in Visual Studio. However, the function is not so complex and the requirements are as follows:
1. You can open the Data Search dialog box through the menu in the main form.
2. the query dialog box is not displayed before the data of the DataGridView is loaded.
3. The Search dialog box supports case-and full-character matching.
4. The Search dialog box is displayed on the main form in a non-mode dialog box.
5. Highlight the row where the keyword is located in the DataGridView.
6. you can perform a cyclic query on the data in the DataGridView in the Search dialog box. That is, each time you perform a search, the DataGridView dview will start from the last position until the last row, then start from the first line to continue searching.
7. You can perform a row-by-column query on the DataGridView.
Traverse the DataGridView datagrirow by row, match the keyword, and highlight the current row. This function is easy to implement, and the key is how to implement cyclic search, and can interact well with subforms (Search dialog box. In addition, we need to implement case-insensitive matching and circle matching. here we need to use a regular expression. Let's take a look at the main interface of the program.
The implementation of the main form is not described here. This is not the focus of this Article. Besides, many other functions are implemented in the above program. Here I will mainly introduce the functions of the datagri form and how to implement cyclic data search of the DataGridView dview.
Let's take a look at how to create a relatively beautiful Search dialog box.
For example, you can place the Match case and Match whole word controls used to set query parameters in a layout control, such as GroupBox. In this way, the interface looks more professional. Then you need to set parameters for the child form to make it look more like a dialog box.
FormBorderStyle: FixedDialog
Text: Find Record
Name: FindRecord
StartPosition: CenterScreen
AcceptButton: BtFindNext (Find Next button)
CancelButton: BtCancel (Cancel button)
MaximizeBox: False
MinimizeBox: False
ShowIcon: False
ShowInTaskbar: False
TopMost: True
Add some functions to the dialog box
The dialog box should be globally valid. Otherwise, we cannot record the Index of the hit record in the DataGridView after each query. Therefore, the instance of the dialog box form should be initialized in the main form and instantiated only once. Each time a dialog box is opened, the Show () method of the instance is called. When the dialog box is closed, only the Hide () method of the form is called instead of the Close () method, because Close () method will cancel the instance of the form in the memory. Then we need to define the btCancel button event and override the form's FormClosing event and call the form's Hide () method in it.
The case-and full-character match in the query parameters are check box controls, which means there are multiple combinations of parameters. You may wish to define these combinations as an enumeration. There are four cases in total: any match (None), case match (MatchCase), full match (MatchWholeCase), case match and full match (MatchCaseAndWholeWord ).
It is no good to use the event model for data search. First, define an EventHandler in the query dialog box, and subscribe to this event in the main form. The event Execution Code is written to the btFindNext button event of the sub-form, and a total of three parameters are passed: the query content, the current row number of the DataGridView (used to locate the next search), and the enumerated variable of the query parameter. The specific implementation code of the sub-form is as follows:
1 Using System;
2 Using System. Collections. Generic;
3 Using System. ComponentModel;
4 Using System. Data;
5 Using System. Drawing;
6 Using System. Linq;
7 Using System. Text;
8 Using System. Windows. Forms;
9
10 Namespace ListItemEditor. UI
11 {
12 Public Partial Class FindRecord: Form
13 {
14 Public EventHandler < Findrecordw.weventargs > OnFindClick = Null ;
15 Public Enum FindOptions {None, MatchCase, MatchWholeWord, MatchCaseAndWholeWord}
16 Public Int CurrentIndex = - 1 ;
17
18 Public FindRecord ()
19 {
20 InitializeComponent ();
21 }
22
23& Nb