Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Problem with the Return statement

Posted on 2014-02-03
4
192 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Get number of Files in Directory and Sub Directories 2 48
Expression Evaluater 3 38
Syntax Issue with SSIS module 26 104
reading excel file in .net 2 18
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

829 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