C#.net winform reportviewer toolbar add button

Sushil Agarwal 381 Reputation points
2022-09-18T14:32:21.017+00:00

Der experts,
i am using visual studio 2022 and develop windows forms.

i use reportviewer, how can i add a email send button to it ?

i tried calling follwing addcontrol but it hides the reportviewer completly

can somebody guide, how to do it?

#region sendEmail          
        private void AddControl(ReportViewer oCollection)  
        {  
            //oCollection = reportViewer1.LocalReport  
            using (ToolStrip ts = (ToolStrip)oCollection.Controls.Find("toolStrip1", true)[0])  
            {  
                ToolStripMenuItem btneMail = new ToolStripMenuItem();  
                btneMail.Name = oCollection.Name.ToString();  
                btneMail.Image = Properties.Resources.emailblue;  
                btneMail.ToolTipText = "Send E-Mail";  
                ts.Items.Add(btneMail);  
                btneMail.Click += sendmail;  
            }  
        }  
        private void sendmail(System.Object sender, System.EventArgs e)  
        {  
            string rptname = ((ToolStripMenuItem)sender).Name;  
  
            ReportViewer rpt = (ReportViewer)Controls.Find(rptname, true)[0];  
  
            //Modelform-- userforms, chnage as per your requirment  
            //MailTo frm = new MailTo(rpt.LocalReport, this.Text);  
            //if (rpt.Name == "ReportViewer1")  
            //    frm.TextBox2.Text = Txtcity.Tag.ToString;  
  
            //frm.StartPosition = FormStartPosition.CenterScreen;  
            //frm.ShowDialog();  
        }  
        #endregion sendEmail          
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,827 questions
0 comments No comments
{count} votes

Accepted answer
  1. Reza Aghaei 4,936 Reputation points MVP
    2022-09-19T19:42:04.883+00:00

    The main problem in your code is the using statement which disposes the toolbar. You should not use using. Also, you need to look into controls collection of the ReportViewer.

    Here is the code, for example:

    private void ReportForm_Load(object sender, EventArgs e)  
    {  
        // Load your data from wherever your data is  
        // Set data to report  
        // Refresh report  
      
        // Then add the button to Toolbar of ReportViewr  
        ToolStrip toolStrip = (ToolStrip)reportViewer1.Controls.Find("toolStrip1", true)[0];  
        toolStrip.Items.Add(new ToolStripButton("Send Email", /*image*/ null, SendEmail_Click));  
    }  
      
    void SendEmail_Click(object sender, EventArgs e)  
    {  
        //Send email here      
    }  
    

1 additional answer

Sort by: Most helpful
  1. Jack J Jun 24,286 Reputation points Microsoft Vendor
    2022-09-19T07:55:49.483+00:00

    @Sushil Agarwal , Welcome to Microsoft Q&A, based on my test, I reproduced your problem. And according to my research, it is not possible to customize the report viewer toolbar.

    This is the suggested route by Microsoft from the doc Configuring and Using the ReportViewer Toolbar:

    If you want a different toolbar implementation, you can create a custom toolbar to replace the default toolbar.

    You could try to create a new custom toolstrip and add the control you wanted, like the following:

    Code:

       ToolStripMenuItem btneMail = new ToolStripMenuItem();  
        btneMail.Name = reportViewer1.Name.ToString();  
        btneMail.ToolTipText = "Send E-Mail";  
        toolStrip1.Items.Add(btneMail);  
        reportViewer1.ShowToolBar = false;  
        reportViewer1.Controls.Add(toolStrip1);  
    

    Result:

    242454-image.png

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments