9 Replies - 517 Views - Last Post: 23 July 2010 - 05:16 PM Rate Topic: -----

#1 megglz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

referencing gridviews by names in codebehind

Posted 23 July 2010 - 05:54 AM

I have a method I want to use on two gridviews which contain different information, ideally i want to do an if statement, something like


if (GridView.ID = "GridView_ABC") {

// do this

}

else if (GridView.ID = "GridView_2") {

// do this

}





is this possible in some way?
Is This A Good Question/Topic? 0
  • +

Replies To: referencing gridviews by names in codebehind

#2 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: referencing gridviews by names in codebehind

Posted 23 July 2010 - 06:42 AM

Best way to find out is first hand :)
Try it and find out if it works! If you run into problems ask ;)

-Frinny
Was This Post Helpful? 1
  • +
  • -

#3 megglz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

Re: referencing gridviews by names in codebehind

Posted 23 July 2010 - 06:51 AM

I already did, but the only options I have if I type 'GridView.' are Equals and ReferenceEquals. Maybe reference equals?? Hmmm

Edit: Nope
Was This Post Helpful? 0
  • +
  • -

#4 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: referencing gridviews by names in codebehind

Posted 23 July 2010 - 07:12 AM

Well it would help if you mentioned what method you are attempting to do this within.
Somehow you need to get a reference to the specific GridView that you're working with. Using that instance you can access the GridView's properties, methods etc.

Just typing GridView will give you access to the GridView Type. It's not very helpful if you want to access the properties of a specific instance of a GridView (which is an Object).

-Frinny
Was This Post Helpful? 0
  • +
  • -

#5 megglz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

Re: referencing gridviews by names in codebehind

Posted 23 July 2010 - 07:29 AM

I'm trying to check if the grid's time field contains this month or the previous month.

here it is, working for one grid

           protected bool rowFromPrevMonth(GridViewRow r)
            {
                 DateTime dt = DateTime.Now.AddMonths(-1);
                string currentMonth = dt.ToString("MMMM");
                string year = String.Format("{0:yyyy}", DateTime.Now);
                prevMonth += " " + year;
         
                string prev = ((HiddenField)r.Cells[2].FindControl("HiddenTime")).Value;
 
               if (prevMonth == prev)
                {
                    return true;
                }

                return false;
            }


Was This Post Helpful? 0
  • +
  • -

#6 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: referencing gridviews by names in codebehind

Posted 23 July 2010 - 07:35 AM

Use the look through the GridViewRow Members for something that would help you get a reference to the GridView that it is part of (try the Parent Property for example). For some reason there is something tugging at my memory about using the naming container but...not sure if it would help you much in this case. I think the Parent property will do fine :)

-Frinny
Was This Post Helpful? 2
  • +
  • -

#7 megglz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

Re: referencing gridviews by names in codebehind

Posted 23 July 2010 - 07:43 AM

Cheers, Frinny, will investigate :) Have a good weekend
Was This Post Helpful? 0
  • +
  • -

#8 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: referencing gridviews by names in codebehind

Posted 23 July 2010 - 07:51 AM

Pass the GridView ID as a parameter to your method. Then you can determine which GridView it is.

Example:
protected bool rowFromPrevMonth(string gvID, GridViewRow r)
 {

     if (gvID == "GridView_ABC") {

        // do this
      }
      else if (gvID == "GridView_2") {

        // do this
     }

     DateTime dt = DateTime.Now.AddMonths(-1);
     string currentMonth = dt.ToString("MMMM");
     string year = String.Format("{0:yyyy}", DateTime.Now);
     prevMonth += " " + year;

     string prev = ((HiddenField)r.Cells[2].FindControl("HiddenTime")).Value;

    if (prevMonth == prev)
     {
         return true;
     }

     return false;
 }

Was This Post Helpful? 1
  • +
  • -

#9 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: referencing gridviews by names in codebehind

Posted 23 July 2010 - 08:22 AM

Yup that's probably the easiest way to accomplish this...but then doesn't get to play with the Parent property! ;)

-Frinny
Was This Post Helpful? 1
  • +
  • -

#10 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: referencing gridviews by names in codebehind

Posted 23 July 2010 - 05:16 PM

So true.

If she does choose to get a reference to the control from the GridViewRow, either the Parent property or the NamingContainer will both work equally well.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1