简体   繁体   中英

Get the column names in a GridView with dynamic columns (Pivot query) on row databound

I'm generating a Gridview using a SQL Datasource. The data source has a stored procedure with uses a PIVOT to generate date-based column names. I want to make each cell clickable (based on the text in the column), but having trouble finding the column name.

I tried a few things like checking for Row[0]. Ideally, I'd want to pass the value of a cell based on the first column of the row and the column header to an update panel, but first I need to know what the column header text is of the cell I'm creating.

if (e.Row.RowType != DataControlRowType.Header)
    {

        string item = e.Row.Cells[0].Text;
        string column;

        for (int j = 0; j < e.Row.Cells.Count; j++)
        {
            column = GridView1.HeaderRow.Cells[j].Text;

            if (j >= 3)
            {


                e.Row.Cells[j].Font.Italic = true;
                e.Row.Cells[j].Text = e.Row.Cells[j].Text.Replace(".00", "");

                //if (GridView1.HeaderRow.Cells[j].Text.Substring(0,3) == "SKU")
                //{

                    e.Row.Cells[j].Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
                    e.Row.Cells[j].Attributes["onmouseout"] = "this.style.textDecoration='none';";
                    e.Row.Cells[j].ToolTip = "Click to select cell";
                    e.Row.Cells[j].Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
                //}

If you want use the value in HTML Code you can do this:

     if (String.Equals(((VALUETYPE)e.Row.FindControl("IDCONTROLHTML")).Value.ToString(), "SOMEVALUE"))

Hope this works dude!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM