Solved

Problem with the Return statement

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
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 video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

914 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

18 Experts available now in Live!

Get 1:1 Help Now