question

SushilAgarwal-8271 avatar image
0 Votes"
SushilAgarwal-8271 asked SushilAgarwal-8271 edited

C#.net winform reportviewer toolbar add button

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

reza-aghaei avatar image
0 Votes"
reza-aghaei answered SushilAgarwal-8271 edited

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks reza-aghaei.

how can i get rendered report i.e. to be e-mailed

what are the best ways to get the e-mail id details from the rendered report i.e how does i should set report parametrs and read them so that here in this event i can use it to send e-mail

0 Votes 0 ·
JackJJun-MSFT avatar image
0 Votes"
JackJJun-MSFT answered

@SushilAgarwal-8271, 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.


image.png (3.5 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.