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);
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
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!