Solved

Problem with the Return statement

Posted on 2014-02-03
4
188 Views
Last Modified: 2014-02-03
Hi,

I have this code below to show de SaveFileDialog when these requirements are not met:

   if (gvSelected != null && gvSelected.DataRowCount == 0) return;

But after pressing on the button to test it still shows de SaveFileDialog.

What do I do wrong?

   private void bbExport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            GridView gvSelected = null;
            if (tbclMain.SelectedTabPage.Equals(tbshGlucs)) gvSelected = gridView1;
            else if (tbclMain.SelectedTabPage.Equals(tbshSupp)) gvSelected = gridView2;
            else if (tbclMain.SelectedTabPage.Equals(tbshFavor)) gvSelected = gridView3;
            if (gvSelected != null && gvSelected.DataRowCount == 0) return;
            SaveFileDialog saveFD = new SaveFileDialog();
            saveFD.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            saveFD.Filter = "txt files (*.txt)|*.txt|csv files (*.csv)|*.csv|xls files (*.xls)|*.xls|pdf files (*.pdf)|*.pdf|HTML files (*.htm)|*.htm";
            saveFD.DefaultExt = ".txt";
            if (saveFD.ShowDialog() == DialogResult.OK)
            {
                switch (saveFD.FilterIndex)
                {
                    case 1:
                        try
                        { gvSelected.ExportToText(saveFD.FileName); }
                        catch (Exception ex)
                        { XtraMessageBox.Show(ex.ToString()); }
                        break;
                    case 2:
                        try
                        { gvSelected.ExportToCsv(saveFD.FileName); }
                        catch (Exception ex)
                        { XtraMessageBox.Show(ex.ToString()); }
                        break;
                    case 3:
                        try
                        { gvSelected.ExportToXls(saveFD.FileName); }
                        catch (Exception ex)
                        { XtraMessageBox.Show(ex.ToString()); }
                        break;
                    case 4:
                        try
                        { gvSelected.ExportToPdf(saveFD.FileName); }
                        catch (Exception ex)
                        { XtraMessageBox.Show(ex.ToString()); }
                        break;
                    case 5:
                        try
                        { gvSelected.ExportToHtml(saveFD.FileName); }
                        catch (Exception ex)
                        { XtraMessageBox.Show(ex.ToString()); }
                        break;
                }
            }
        }

Open in new window


Peter
0
Comment
Question by:peterkiers
  • 2
4 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
Comment Utility
Try this:
if (gvSelected == null || gvSelected.DataRowCount == 0) return;

Open in new window

0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
put a breakpoint on the line:
SaveFileDialog saveFD = new SaveFileDialog();

Now have a look at what the values of gvSelected are (If it is null then the return will not be called because of your && in the if statement - did you want an or || at that point? )
0
 
LVL 35

Expert Comment

by:Robert Schutt
Comment Utility
PS: explanation: you want to continue with the code when 1 of the 3 grids is selected and the selected grid has rows. So you want to exit if the opposite is true, with boolean logic applied this means both parts are negated and operators and/or are switched around, so
if (!(gvSelected != null && gvSelected.DataRowCount != 0)) return;

Open in new window

would be the same but can be shortened to what I posted before. In your code both parts needed to be true but that meant you didn't check for the situation that none of the three grids were selected.
0
 
LVL 1

Author Comment

by:peterkiers
Comment Utility
Thanks. Peter
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now