문제

나는 그리드 뷰에 새 헤드 테라를 추가하려고합니다. 이 행은 원래 헤드 레 테로 아래에 나타납니다.

내가 아는 한 두 가지 이벤트가 있습니다.

1.) gridview_rowdatabound 2.) gridview_rowcreated

그리드가 각 포스트 백의 데이터를 바인딩하지 않으므로 옵션 1은 옵션이 아닙니다. 옵션 2는 예상대로 작동하지 않습니다. 행을 추가 할 수는 있지만 헤드 레터로 자체가 아직 추가되지 않았기 때문에 헤드 레포로 앞에 추가됩니다.

제발 도와주세요, 감사합니다!

코드 : (Innertable 속성은 사용자 정의 GridView에 의해 노출 됨)

    Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
    If e.Row.RowType = DataControlRowType.Header Then
        Dim r As New GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal)

        For Each c As DataControlField In CType(sender, GridView).Columns
            Dim nc As New TableCell
            nc.Text = c.AccessibleHeaderText
            nc.BackColor = Drawing.Color.Cornsilk
            r.Cells.Add(nc)
        Next

        Dim t As Table = GridView1.InnerTable
        t.Controls.Add(r)
    End If
End Sub
도움이 되었습니까?

해결책

이것은 사용자 정의 GridView이므로 CreateChildControls 메소드를 재정의하는 것을 고려하지 않겠습니까?

IE (죄송합니다, C#) :

protected override void CreateChildControls()
{
    base.CreateChildControls();

    if (HeaderRow != null)
    {
        GridViewRow header = CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
        for (int i = 0; i < Columns.Count; i++)
        {
            TableCell cell = new TableCell();
            cell.Text = Columns[i].AccessibleHeaderText;
            cell.ForeColor = System.Drawing.Color.Black;
            cell.BackColor = System.Drawing.Color.Cornsilk;
            header.Cells.Add(cell);
        }

        Table table = (Table)Controls[0];
        table.Rows.AddAt(1, header);
    }
}

업데이트로프 스타 (Ropstah)가 언급했듯이 위의 저격수는 페이지 매김과 함께 작동하지 않습니다. 나는 코드를 준비 대동맥으로 옮겼으며 이제는 페이지 매김, 선택 및 정렬과 함께 우아하게 작동합니다.

protected override void PrepareControlHierarchy()
{
    if (ShowHeader && HeaderRow != null)
    {
        GridViewRow header = CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
        for (int i = 0; i < Columns.Count; i++)
        {
            TableCell cell = new TableCell();
            cell.Text = Columns[i].AccessibleHeaderText;
            cell.ForeColor = System.Drawing.Color.Black;
            cell.BackColor = System.Drawing.Color.Cornsilk;
            header.Cells.Add(cell);
        }

        Table table = (Table)Controls[0];
        table.Rows.AddAt(1, header);
    }

    //it seems that this call works at the beginning just as well
    //but I prefer it here, since base does some style manipulation on existing columns
    base.PrepareControlHierarchy();
}

다른 팁

좋은 일을했는데, 나는 당신의 기술을 Ajax 지원 Gridview를 그룹화하는 데 사용했으며 오랫동안 오랫동안 검색했습니다. 건배.

protected override void PrepareControlHierarchy()
{
    if (GroupColumns)
    {
        #region Group Column

        Table table = (Table)Controls[0];

        string lastValue = string.Empty;
        foreach (GridViewRow gvr in this.Rows)
        {
            string currentValue = gvr.Cells[GroupColumnIndex].Text;

            if (lastValue.CompareTo(currentValue) != 0)
            {
                // there's been a change in value in the sorted column
                int rowIndex = table.Rows.GetRowIndex(gvr);

                // Add a new sort header row
                GridViewRow sortRow = new GridViewRow(rowIndex, rowIndex, DataControlRowType.DataRow, DataControlRowState.Normal);

                TableCell sortCell = new TableCell();
                TableCell blankCell = new TableCell();

                sortCell.ColumnSpan = this.Columns.Count - 1;
                sortCell.Text = string.Format("{0}", currentValue);

                blankCell.CssClass = "group_header_row";
                sortCell.CssClass = "group_header_row";

                // Add sortCell to sortRow, and sortRow to gridTable
                sortRow.Cells.Add(blankCell);
                sortRow.Cells.Add(sortCell);
                table.Controls.AddAt(rowIndex, sortRow);

                // Update lastValue
                lastValue = currentValue;
            }
        }

        #endregion
    }

    HideColumns();

    base.PrepareControlHierarchy();
} 

innertable에 행을 추가 할 때 이것을 시도하십시오.

t.Controls.AddAt(1, r)

다음은 내가 한 빠른 기본 테스트입니다.

Protected Sub gridview_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles gridview.DataBound
    Dim g As GridView = CType(sender, GridView)

    Dim r As New GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Normal)
    Dim th As New TableHeaderCell()
    th.ColumnSpan = g.Columns.Count
    th.Text = "This is my new header"
    r.Cells.Add(th)

    Dim t As Table = CType(g.Controls(0), Table)
    t.Rows.AddAt(1, r)
End Sub
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top