About Me

My photo
Software Developer and ideas man!

Sunday, November 28, 2010

How to add a toolbar item to the Crystal Reports Viewer Toolbar

I was looking for a way to add a "Filter" button to the Crystal Report Viewer toolbar so that we could change the records used when generating a report chart at Runtime.

I thought this would be a straight forward task to be able to customize the Crystal Report Viewer toolbar in the edition of CReports shipped with Visual Studio 2008. To my surprise, this took about 45 minutes to nut out the classes and properties and cobble something together which I thought I would share.

1. Firstly, you need to cycle through the Crystal Report viewer controls looking for the Toolstrip. I iterated through them but a Linq statement would be equally effective.

2. Once you have the ToolStrip control, create a Toolstrip item to include in the Toolstrip, including assigning a "Click" event handler.

3. Finally, find an appropriate spot in the form code such as form_shown() event handler to call this function.

Below is the code I used.

private void AddFilterButton()
{
  foreach (Control c in this.CRViewer.Controls)
  {
   if (c is ToolStrip)
   {
     ToolStripButton BtnFilter = new ToolStripButton();
     BtnFilter.Name = "BtnFilter";
     BtnFilter.Text = "Filter...";
     BtnFilter.ToolTipText = "Filter Report Records";

   
     BtnFilter.Click += new EventHandler(BtnFilter_Click);

     (c as ToolStrip).Items.Add(BtnFilter);
     break;
   }
  }
}

The event handler

void BtnFilter_Click(object sender, EventArgs e)
{
  frmFilterProperties frmFilterProperties = new    frmFilterProperties(this.CRViewer.ReportSource);
  if (frmFilterProperties.ShowDialog() == DialogResult.OK)
  {
   //Do something here
  };

  frmFilterProperties.Dispose();
}

Cheers,

Paul Power
P.S. Feel free to add suggestions or feedback - would be great to hear if someone else found this useful.


More from this Blogger:
Localizing Crystal Reports in C# - I demonstrate one way of doing it despite the hurdles!

2 comments:

  1. Hi Paul,

    Thank you for the post. It is quite interesting.

    I have got small problem with CRViewr V10.5

    Is there any way to get selected value from the viewer?

    Thank you:
    Peter

    ReplyDelete
  2. Hey Peter,

    I have not tried this myself but I did find a link that you may find useful:

    http://social.msdn.microsoft.com/Forums/en-US/vscrystalreports/thread/7eb2fa35-fc17-4651-a8a4-3278ca362769/

    If it is not, the guys on the http://forums.sdn.sap.com/forum.jspa?forumID=313
    have provided some good support for me in the past.

    Let us know how you go,

    Paul

    ReplyDelete