Hide Datagrid columns Question using visible

Hi all.  Ok here is what I need to do.  My datagrid has a total of 18 columns.  I have a count that will be between 1 and 5.  What I need to do is based on the count I need to make not visible a set of two columns starting from the last columns 18.  If my count is 5 then all 18 columns will remain visible.  However for each 1 less in the count I need to hide two columns.  For example,

Lets say my count is 4.  I then need to hide the last two columns such as:
dgResults.Columns[18].Visible = false;
dgResults.Columns[17].Visible = false;

then if the count is 3 then:
dgResults.Columns[18].Visible = false;
dgResults.Columns[17].Visible = false;
dgResults.Columns[16].Visible = false;
dgResults.Columns[15].Visible = false;

What is the best way to code this to make it clean?  Thanks all
sbornstein2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

quoclanCommented:
You can code like this:

private int numberOfColumnHided(int count)
            {
                  int ret = -1;
                  switch (count)
                  {
                        case 5:
                        case 4:
                        case 3:
                        case 2:
                        case 1:
                              ret = (5-count)*2;
                              break;
                        default:      //wrong count
                              ret = -1;
                  }
                  return ret;
            }

//in your method
//.....
int n = numberOfColumnHided(count);
                  if (n==-1)      
                  {
                  //wrong
                  }
                  else
                        for (int i=18;i>18-n;i--)
                              dgResults.Columns[i].Visible = false;
//......

Hope this help you.
0
sbornstein2Author Commented:
sorry little confused.  I have Ordercount as a number coming into my page load.  Where do I pass that to?
0
AGBrownCommented:
You could try a different method to get the cutoff:

//      Get the cutoff - i.e., the zero-based index of the first hidden column
int intCutoff = myGrid.Columns.Count - (2 * (5 - Ordercount));
//    loop through the columns
for (int i = 0; i < myGrid.Columns.Count; i++)
{
      myGrid.Columns[i].Visible = (i < intCutoff);
}

By iterating over all the columns, this will ensure that your Visible property is reset properly each time you change Ordercount. If that isn't necessary you can use something like quoclan's method, but I would be tempted just to get n using the intCutoff equation above rather than using a switch.
for (int i = intCutoff; i < myGrid.Columns.Count; i++)
{
      myGrid.Columns[i].Visible = false;
}

If there is a possibility of Ordercount being wrong, you would handle it by:
if (Ordercount > 5 || Ordercount < 0)
    throw new Exception();

Let me know if you want a VB sample instead of C#.

Andy
0
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

AGBrownCommented:
Actually, looking again, the switch statement given by quoclan will give you an error as you can't fall through from case to case, and you must have a jump statement in each block, so if you go with that one, then you will need:

      switch (count)
      {
            case 5:
                  goto case 1;
            case 4:
                  goto case 1;
            case 3:
                  goto case 1;
            case 2:
                  goto case 1;
            case 1:
                  ret = (5-count)*2;
                  break;
            default:     //wrong count
                  ret = -1;
                  break;
      }

A
0
quoclanCommented:
Oh, sorry :), like that:

private int numberOfColumnHided(int count)
          {
               int ret = -1;
               switch (count)
               {
                    case 5:
                    case 4:
                    case 3:
                    case 2:
                    case 1:
                         ret = (5-count)*2;
                         break;
                    default:     //wrong count
                         ret = -1;
               }
               return ret;
          }

//your method hide column(s)
//.....
int n = numberOfColumnHided(Ordercount);    //pass Ordercount here
               if (n==-1)    
               {
               //wrong
               }
               else
                    for (int i=18;i>18-n;i--)
                         dgResults.Columns[i].Visible = false;
//......
0
AGBrownCommented:
Actually (sorry to spam) but I think there's another point that you need to looking at. You say you "have 18 columns". If that is true, then I think I am correct in saying that arrays are 0-based, and the Columns collection's indexer is no exception. The code I have given you is for zero-based columns. If you have a select column in your grid, at position 0, I don't know if you are including this in your total or not.

If you _only_ have 18 columns then I think you mean:
      For Ordercount = 4
      dgResults.Columns[17].Visible = false;
      dgResults.Columns[16].Visible = false;

      For Ordercount = 3
      dgResults.Columns[17].Visible = false;
      dgResults.Columns[16].Visible = false;
      dgResults.Columns[15].Visible = false;
      dgResults.Columns[14].Visible = false;

In which case quoclan's "for" statement needs changing to:
    for (int i = 17; i >= 17-n ;i--)
        dgResults.Columns[i].Visible = false;

Note that I've changed "i > 18-n" to "i >= 18-n" and then 18 to 17. Given that C# arrays have 0-based indexers, you would otherwise not hide the first column that you wanted to, and you would get an exception on the first column as there is no column 18.

Whether you have 19, or 18 columns, if you do use
      int intCutoff = myGrid.Columns.Count - (2 * (5 - Ordercount));
then you take into account the number of columns anyway; you just hide the last 2 * Ordercount without caring how many there are. This means that it will also work if you change the number of columns in the grid later on, as long as you still want to hid the last 2 * Ordercount.

Andy
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
quoclanCommented:
Oh, sorry :), with case default, it need jump statement like AGBrown said (thanks AGBrown) ... if you don't need case default (to confirm), you can use my code.
0
sbornstein2Author Commented:
thanks guys.  I ended up just using a switch statement and using the column numbers instead.  Thanks though for all your input
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.