C# .NET - gridview with templatefields - Asked By Haritha Jadcherla on 06-May-13 02:18 AM

<asp:GridView ID ="gvStatusSummary" runat="server"  CssClass="SectionContainer" 
                AutoGenerateColumns="false" BorderColor="Black" Width="98%" 
          GridLines="Both" onselectedindexchanged="gvStatusSummary_SelectedIndexChanged">
          <HeaderStyle CssClass="TextSmallBold" HorizontalAlign="Center" BackColor="CornflowerBlue" />
          
         <columns>
          <asp:BoundField HeaderText="Request Type"  DataField="DESCRIPTION" ItemStyle-HorizontalAlign="left" />
          <asp:TemplateField HeaderText="Draft" ItemStyle-HorizontalAlign="Center">
           <ItemTemplate>           
          <asp:LinkButton ID="lnkdraft" runat="server" CommandArgument="<%#((GridViewRow) Container).RowIndex %>" CommandName="Select" Text='<%# Eval("DRAFT") %>'/>
          </ItemTemplate>
          </asp:TemplateField>
            <asp:TemplateField HeaderText="Pending" ItemStyle-HorizontalAlign="Center">
          <ItemTemplate>           
          <asp:LinkButton ID="lnkpending" runat="server" CommandArgument="<%#((GridViewRow) Container).RowIndex %>" CommandName="Select" Text='<%# Eval("PENDING") %>'/>
          </ItemTemplate>
          </asp:TemplateField>
            <asp:TemplateField HeaderText="Approved" ItemStyle-HorizontalAlign="Center">
          <ItemTemplate>           
          <asp:LinkButton ID="lnkApproved" runat="server" CommandArgument="<%#((GridViewRow) Container).RowIndex %>" CommandName="Select" Text='<%# Eval("APPROVED") %>'/>
          </ItemTemplate>
          </asp:TemplateField>
          <asp:BoundField HeaderText="Assigned" DataField="ASSIGNED" ItemStyle-HorizontalAlign="Center"/>
          <asp:BoundField HeaderText="Cancelled" DataField="CANCELLED" ItemStyle-HorizontalAlign="Center"/>
          <asp:BoundField HeaderText="Completed" DataField="COMPLETED" ItemStyle-HorizontalAlign="Center"/>
             </columns>         
         </asp:GridView>    

When I click on these link buttons, the corresponding column header text should be assigned to a label .
For example if I select Row 3 column 2 cell value, the column2 header text should be read. here is my code behind file :
 protected void gvStatusSummary_SelectedIndexChanged(object sender, EventArgs e)
    {
        LabelReqtype.Text = gvStatusSummary.SelectedRow.Cells[0].Text;
        int x = gvStatusSummary.SelectedRow.DataItemIndex;
         for (int index = 1; index < gvStatusSummary.Columns.Count- 1; index++)
         {
             if (index==x)
                LabelStatus.Text =  gvStatusSummary.Columns[index].HeaderText;
             else                
             if (index == (x - 1))
                 LabelStatus.Text =  gvStatusSummary.Columns[x-1].HeaderText;
             else   
             if(index==(x+1))
                 LabelStatus.Text =  gvStatusSummary.Columns[x+1].HeaderText;
                 break;            
        }
         }

I am not able to read the exact column header text rather I am getting the last one or the third column header text. please let me know what wrong I am doing
           
Robbe Morris replied to Haritha Jadcherla on 06-May-13 02:56 PM
You want to remember that arrays/collections/lists in .NET are zero based.  This line looks problematic:

  for (int index = 1; index < gvStatusSummary.Columns.Count- 1; index++)
Haritha Jadcherla replied to Robbe Morris on 06-May-13 03:19 PM
I tried that but how would I know once the header text of the cell I clicked  is read, so I have changed the logic little bit here is the one

 int y = gvStatusSummary.SelectedIndex;        
          
        for (int index = 0; index < gvStatusSummary.Columns.Count - 3; index++)
        {          
          
            if (gvStatusSummary.Columns[index].HeaderText == gvStatusSummary.HeaderRow.Cells[y - 1].Text)
            {
                LabelStatus.Text = gvStatusSummary.HeaderRow.Cells[y - 1].Text;
            }
            else if (gvStatusSummary.Columns[index].HeaderText == gvStatusSummary.HeaderRow.Cells[y].Text)
            {
                LabelStatus.Text = gvStatusSummary.HeaderRow.Cells[y].Text;
            }


            else if (gvStatusSummary.Columns[index].HeaderText == gvStatusSummary.HeaderRow.Cells[y + 1].Text)
            {
                LabelStatus.Text = gvStatusSummary.HeaderRow.Cells[y + 1].Text;               
            } 
               


              if (index == y || index==(y+1))
               break;     
        }