How to access column header text in Gridview

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ASPnewb1
    New Member
    • Mar 2008
    • 14

    #1

    How to access column header text in Gridview

    this is used when the Updating Row is fired


    the first line works in grabbing the text from the textbox of the cell, but I cannot access the column header text, as it's telling me the columns do not exist.

    cName = (System.Web.UI. WebControls.Tex tBox)GridView2. Rows[e.RowIndex].Cells[i].Controls[0]; //Cells[1].Controls[0];


    cName.Text = GridView2.Colum ns[i].HeaderText. + "=" + cName;

    ETA: my columns of data (the first 2 columns were added as an edit and delete column) are added automatically/dynamically, I believe this might be the issue.


    thanks.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    GridViews have a special property called HeaderRow which is where you can get your column text from.

    For example, I search through the HeaderRow to discover which index is the column I am looking for. You should be able to figure out how to get the column text from it:
    Code:
    int retdateidx = -1;
    if (gvOverdueSites.HeaderRow != null)
    {
       for (int i = 0; i < gvOverdueSites.HeaderRow.Cells.Count; i++)
       {
          if (gvOverdueSites.HeaderRow.Cells[i].Text == "Return Date")
          {
             retdateidx = i;
          }
       }
    }
    if (retdateidx != -1)
    {//retdateidx is the column index for the column with the header "Return Date"
    }

    Comment

    Working...