LCJ
LCJ

Reputation: 22661

Read Gridview header value of a dynamically added header

I am adding a table header row dynamically as shown below. It is rendered correctly. Now I need to read the first cell value of the newly added header row in a button click event. How can we read the header value?

I cannot use gvCustomers.Rows since it will not take header row.

I cannot use gvCustomers.HeaderRow.Cells[0].Text; also since there are two header rows

CODE

protected void gvCustomers_RowCreated(object sender, GridViewRowEventArgs e)
{

    if (e.Row.RowType == DataControlRowType.Header)
    {
        GridViewRow newHeaderRow = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);

        TableCell cell1 = new TableHeaderCell();
        cell1.ColumnSpan = 1; //e.Row.Cells.Count;
        cell1.Text = "Expected";

        TableCell cell2 = new TableCell();
        cell2.ColumnSpan = 2;
        cell2.Text = "One";

        TableCell cell3 = new TableCell();
        cell3.ColumnSpan = 2;
        cell3.Text = "Two";

        TableCell cell4 = new TableCell();
        cell4.ColumnSpan = 2;
        cell4.Text = "Three";

        newHeaderRow.Cells.Add(cell1);
        newHeaderRow.Cells.Add(cell2);
        newHeaderRow.Cells.Add(cell3);
        newHeaderRow.Cells.Add(cell4);

        ((GridView)sender).Controls[0].Controls.AddAt(0, newHeaderRow);
    }



}

protected void Btn_Click(object sender, EventArgs args)
{


}

Upvotes: 2

Views: 2354

Answers (1)

LCJ
LCJ

Reputation: 22661

I am using the following approach. Any improvement suggestions?

        int current = 0;
        int headerCount = grdTransactions.HeaderRow.Cells.Count;

        for (current = 0; current < headerCount; current++)
        {
            TableHeaderCell cell = new TableHeaderCell();
            cell.Text = grdTransactions.HeaderRow.Cells[current].Text;
            originalHeaderRow.Cells.Add(cell);
        }

Upvotes: 1

Related Questions