Reader Level:
ARTICLE

Implementing Search in Reports using ReportViewer

On April 30, 2007 Articles | Reports using C#
The ReportViewer control provides Find and FindNext methods to find a text in the report. This article explains how you can implement search functionality in reports programmatically.
  • 0
  • 0
  • 21832
Download Files:
 

The ReportViewer control provides Find and FindNext methods to find a text in the report. The Find method takes two parameters - the text and the starting page number.

Now let's create a Windows Forms application and add a TextBox and a Button control to the form. The button click would search the text entered in the TextBox.

I have Form with a TextBox and a Button control. See Figure 1.



Figure 1.

I definite the following variable in the class:

private bool firstFound = false;

The following code written on the Find Button click event handler.

private void FindButton_Click(object sender, EventArgs e)

{

     if (firstFound)

         this.reportViewer1.FindNext();

     else if (this.reportViewer1.Find(FindTextBox.Text, 1) >= 0)

         firstFound = true;

}

Now if I type "Chand" in the TextBox and click the Find button, the first row is selected. Clicking Find again select the next row that has text Chand in it. See Figure 2.

 

Figure 2.

Download the attached project to see more details and working code.

To learn more about how to generate reports from database, XML documents, and objects, check my articles published in SQL Reporting Services section of this site.

Mahesh Chand

Mahesh Chand is founder of C# Corner. C# Corner founded in 1999 is a FREE member contributions based open platform for developers to solve problems, learn new technology and hang out.  M...

  • 1rank
  • 71M readers
  • 9times
COMMENT USING

Trending up