读取动态添加的标头的Gridview标头值

时间:2012-09-21 08:26:57

标签: c# asp.net .net

我正在动态添加表标题行,如下所示。它正确呈现。现在我需要在按钮单击事件中读取新添加的标题行的第一个单元格值。我们如何读取标题值?

我无法使用 gvCustomers.Rows ,因为它不会占用标题行。

我不能使用 gvCustomers.HeaderRow.Cells [0] .Text ;因为有两个标题行

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)
{


}

1 个答案:

答案 0 :(得分:1)

我使用以下方法。有任何改进建议吗?

        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);
        }