Solved

Problem with the Return statement

Posted on 2014-02-03
4
195 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
[X]
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
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

690 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