Solved

Problem with the Return statement

Posted on 2014-02-03
4
190 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
ID: 39829333
Try this:
if (gvSelected == null || gvSelected.DataRowCount == 0) return;

Open in new window

0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39829338
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
ID: 39829347
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
ID: 39829354
Thanks. Peter
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

816 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

11 Experts available now in Live!

Get 1:1 Help Now