referencing controls in a datagrid query

I am trying to make changes to an existing asp.net web app and am having problems identifying controls (I am a newbie from asp)
The relevant code is :

<asp:DataGrid id="dgLocation" runat="server" BorderWidth="0px" BackColor="White" Width="422px"
      AutoGenerateColumns="False" CellPadding="0">
      <ItemStyle BorderWidth="2px" BorderStyle="Solid" BorderColor="Black"></ItemStyle>
      <Columns>
            <asp:TemplateColumn>
                  <ItemStyle VerticalAlign="Top"></ItemStyle>
                  <ItemTemplate>
                        <IMG height="1" src="Images/clear.gif" width="10" border="0">
                        <asp:Label id="lblUser" runat="server" CssClass="txtSmall"></asp:Label>
                  </ItemTemplate>
            </asp:TemplateColumn>
            <asp:TemplateColumn>
                  <ItemStyle VerticalAlign="Top"></ItemStyle>
                  <ItemTemplate>
                        <asp:LinkButton id="lnkLUser" runat="server" CssClass="txtSmall"></asp:LinkButton>
                        <asp:Label id="lblLUser" runat="server" CssClass="txtSmall"></asp:Label>
                        <asp:ImageButton id="btnLOpen" runat="server" ImageUrl="Images/btn_open.gif" ToolTip="Additionl user must be removed before desk can be made OPEN/SPARE."></asp:ImageButton>
                  </ItemTemplate>
            </asp:TemplateColumn>
            <asp:TemplateColumn>
                  <ItemStyle VerticalAlign="Top"></ItemStyle>
                  <ItemTemplate>
                        <asp:ImageButton id="btnLChangePerson" runat="server" ImageUrl="Images/btn_ChangeUser.gif" ToolTip="Change the Primary user of this Desk."></asp:ImageButton>
                        <asp:ImageButton id="btnLRemovePerson" runat="server" ImageUrl="Images/btn_removeUser.gif" ToolTip="Remove this person as an additional user at this Desk."></asp:ImageButton>
                        <asp:ImageButton id="btnLAddPerson" runat="server" ImageUrl="Images/btn_adduser.gif" ToolTip="Add an additional Person to this Desk. The Order of the occupants can be sorted after the user is added."></asp:ImageButton>
                  </ItemTemplate>
            </asp:TemplateColumn>
            <asp:TemplateColumn>
                  <ItemStyle VerticalAlign="Top"></ItemStyle>
                  <ItemTemplate>
                        <asp:ImageButton id="btnLGeneric" runat="server" ImageUrl="Images/btn_generic.gif" ToolTip="Add a Generic Use to this Desk . i.e. HOTDESK, VISITOR etc."></asp:ImageButton>
                        <asp:ImageButton id="btnLMakePrime" runat="server" ImageUrl="Images/btn_makePrime.gif"></asp:ImageButton>
                        <IMG height="1" src="Images/clear.gif" width="10" border="0">
                  </ItemTemplate>
            </asp:TemplateColumn>
            <asp:BoundColumn Visible="False" DataField="msfwid"></asp:BoundColumn>
            <asp:BoundColumn Visible="False" DataField="rowid"></asp:BoundColumn>
            <asp:BoundColumn Visible="False" DataField="WholeName"></asp:BoundColumn>
            <asp:BoundColumn Visible="False" DataField="PrimeUser"></asp:BoundColumn>
            <asp:BoundColumn Visible="False" DataField="Locked"></asp:BoundColumn>
            <asp:BoundColumn Visible="False" DataField="Generic"></asp:BoundColumn>
      </Columns>
</asp:DataGrid>
My problem is that I don't understand the code that governs this datagrid. There are a complex set of rules as to which of the buttons should show, depending on the data retrieved. Unfortunately the original developers did not annotate the work so it is difficult for me to follow. A sample of the code behind is :

if(dgLocation.Items.Count == 2)
{
     if((((LinkButton)dgLocation.Items[0].Cells[1].Controls[1]).Text.ToUpper() !="OPEN")&&
          (((Label)dgLocation.Items[0].Cells[1].Controls[3]).Text.ToUpper() !="OPEN"))
     {
          dgLocation.Items[0].Cells[1].Controls[5].Visible = true;                                  
         
          if(dgLocation.Items[0].Cells[8].Text == "True")
               dgLocation.Items[0].Cells[2].Controls[1].Visible = false;                                                            
          else
               dgLocation.Items[0].Cells[2].Controls[1].Visible = true;

          ((ImageButton)dgLocation.Items[0].Cells[2].Controls[1]).Attributes.Add("onclick","popPersonnelList('"+ dgLocation.Items[0].Cells[4].Text +"','"+ ((Label)(dgLocation.Items[0].Cells[0].Controls[1])).Text.Replace(":","") +"','1')");
          dgLocation.Items[0].Cells[2].Controls[3].Visible = false;
          dgLocation.Items[0].Cells[2].Controls[5].Visible = false;
         
          btnEditCC.Visible = false;
     }                                        
     else
     {
          blCheck = true;
          dgLocation.Items[0].Cells[1].Controls[5].Visible = false;                                  
         
          dgLocation.Items[0].Cells[2].Controls[1].Visible = false;                                                                                                        
          dgLocation.Items[0].Cells[2].Controls[3].Visible = false;
          dgLocation.Items[0].Cells[2].Controls[5].Visible = true;
          ((ImageButton)dgLocation.Items[0].Cells[2].Controls[5]).Attributes.Add("onclick","popPersonnelList('"+ dgLocation.Items[0].Cells[4].Text +"','"+ ((Label)(dgLocation.Items[0].Cells[0].Controls[1])).Text.Replace(":","") +"','1')");

          dgLocation.Items[1].Cells[0].Controls[1].Visible = false;                                                                                    
          dgLocation.Items[1].Cells[1].Controls[1].Visible = false;
          dgLocation.Items[1].Cells[1].Controls[3].Visible = false;
          dgLocation.Items[1].Cells[1].Controls[5].Visible = false;                                        
          dgLocation.Items[1].Cells[2].Controls[1].Visible = false;
          dgLocation.Items[1].Cells[2].Controls[3].Visible = false;
          dgLocation.Items[1].Cells[2].Controls[5].Visible = false;                                        
          dgLocation.Items[1].Cells[3].Controls[1].Visible = false;
          dgLocation.Items[1].Cells[3].Controls[3].Visible = false;                                                                                                        
         
          btnEditCC.Visible = true;

          btnEditCC.Attributes.Add("onclick","popCostCenterOptions('"+ lblAddress.Text +"','"+ ((Label)dgLocation.Items[0].Cells[1].Controls[3]).Text.ToUpper() +"')");
     }
}
else
{
     dgLocation.Items[0].Cells[1].Controls[5].Visible = false;                              
     
     if(dgLocation.Items[0].Cells[8].Text == "True")
          dgLocation.Items[0].Cells[2].Controls[1].Visible = false;                                                            
     else
          dgLocation.Items[0].Cells[2].Controls[1].Visible = true;
     
     ((ImageButton)dgLocation.Items[0].Cells[2].Controls[1]).Attributes.Add("onclick","popPersonnelList('"+ dgLocation.Items[0].Cells[4].Text +"','"+ ((Label)(dgLocation.Items[0].Cells[0].Controls[1])).Text.Replace(":","") +"','1')");
     dgLocation.Items[0].Cells[2].Controls[3].Visible = false;
     dgLocation.Items[0].Cells[2].Controls[5].Visible = false;

     btnEditCC.Visible = false;
}

Please can someone explain to me how the controls are numbered. I notice that they all have an index number of 1,3 or 5. There is never a reference to 2 nor 4. How does the Item[].Cell[].Control[] system work?

Thanks in advance
Paul
spaceplannerAsked:
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.

Edwin_CCommented:
Hi Paul

Items[n]  means the n+1 th row in the datagrid.  E.g. Items[0] is the 1st row.

Cells[m]  means the m+1 th table cell in the row.  E.g. Items[0].Cells[0] is the first cell of the first row.

Controls[q] means the q+1 the web control in a cell.  Since ASP.NET datagrid automatically insert a literal control in a cell, the user control always start from Controls[1].  E.g. Items[0].Cells[0].Controls[1] means the 1st user control appeared in the 1st cell of the 1st row.

<asp:DataGrid id="dgLocation" runat="server" BorderWidth="0px" BackColor="White" Width="422px"
     AutoGenerateColumns="False" CellPadding="0">
     <ItemStyle BorderWidth="2px" BorderStyle="Solid" BorderColor="Black"></ItemStyle>
     <Columns>
          <asp:TemplateColumn>  <<==== this is the start of the 1st column i.e. 1st cell of a row.
               <ItemStyle VerticalAlign="Top"></ItemStyle>
               <ItemTemplate>
                    <IMG height="1" src="Images/clear.gif" width="10" border="0">  <<== this is the first control
                    <asp:Label id="lblUser" runat="server" CssClass="txtSmall"></asp:Label>
               </ItemTemplate>
          </asp:TemplateColumn>
          <asp:TemplateColumn>
               <ItemStyle VerticalAlign="Top"></ItemStyle>
               <ItemTemplate>
                    <asp:LinkButton id="lnkLUser" runat="server" CssClass="txtSmall"></asp:LinkButton>  <<== Items[0].Cells[1].Controls[1]
                    <asp:Label id="lblLUser" runat="server" CssClass="txtSmall"></asp:Label>  
                    <asp:ImageButton id="btnLOpen" runat="server" ImageUrl="Images/btn_open.gif" ToolTip="Additionl user must be removed before desk can be made OPEN/SPARE."></asp:ImageButton>
               </ItemTemplate>
          </asp:TemplateColumn>
 

Hope that helps

Edwin
spaceplannerAuthor Commented:
Hi Edwin,
Thanks for your swift response.

Does ASP.NET datagrid automatically insert a literal control before each user control?

So that, in the 3rd TemplateColumn

<asp:TemplateColumn>
               <ItemStyle VerticalAlign="Top"></ItemStyle>
               <ItemTemplate>
                    <asp:ImageButton id="btnLChangePerson" runat="server" ImageUrl="Images/btn_ChangeUser.gif" ToolTip="Change the Primary user of this Desk."></asp:ImageButton><<== Items[0].Cells[2].Controls[1]

                    <asp:ImageButton id="btnLRemovePerson" runat="server" ImageUrl="Images/btn_removeUser.gif" ToolTip="Remove this person as an additional user at this Desk."></asp:ImageButton><<== Items[0].Cells[2].Controls[3]

                    <asp:ImageButton id="btnLAddPerson" runat="server" ImageUrl="Images/btn_adduser.gif" ToolTip="Add an additional Person to this Desk. The Order of the occupants can be sorted after the user is added."></asp:ImageButton><<== Items[0].Cells[2].Controls[5]

               </ItemTemplate>
          </asp:TemplateColumn>


Also, in this example, which control is Item[n].Cell[1].Control[5] ?

thanks in advance
Paul
Edwin_CCommented:
You are correct.

<asp:DataGrid id="dgLocation" runat="server" BorderWidth="0px" BackColor="White" Width="422px"
     AutoGenerateColumns="False" CellPadding="0">
     <ItemStyle BorderWidth="2px" BorderStyle="Solid" BorderColor="Black"></ItemStyle>
     <Columns>
          <asp:TemplateColumn>
               <ItemStyle VerticalAlign="Top"></ItemStyle>
               <ItemTemplate>
                    <IMG height="1" src="Images/clear.gif" width="10" border="0">
                    <asp:Label id="lblUser" runat="server" CssClass="txtSmall"></asp:Label>
               </ItemTemplate>
          </asp:TemplateColumn>
          <asp:TemplateColumn>
               <ItemStyle VerticalAlign="Top"></ItemStyle>
               <ItemTemplate>
                    <asp:LinkButton id="lnkLUser" runat="server" CssClass="txtSmall"></asp:LinkButton>
                    <asp:Label id="lblLUser" runat="server" CssClass="txtSmall"></asp:Label>
                    <asp:ImageButton id="btnLOpen" runat="server" ImageUrl="Images/btn_open.gif" ToolTip="Additionl user must be removed before desk can be made OPEN/SPARE."></asp:ImageButton>  <<=== Item[n].Cell[1].Control[5]
               </ItemTemplate>
          </asp:TemplateColumn>

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
spaceplannerAuthor Commented:
Thanks very much for your help Edwin. I think I understand it now

regards
Paul
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
ASP.NET

From novice to tech pro — start learning today.