developer tip

Gridview가 THEAD를 렌더링하도록하려면 어떻게해야합니까?

optionbox 2020. 7. 30. 10:20
반응형

Gridview가 THEAD를 렌더링하도록하려면 어떻게해야합니까?


태그 GridView를 렌더링하는 컨트롤을 어떻게 얻 <thead> <tbody>습니까? 나는 대신 .UseAccessibleHeaders그것을 넣는다는 것을 알고 있지만, 나타날 수 없다.<th><td><thead>


이것은해야합니다 :

gv.HeaderRow.TableSection = TableRowSection.TableHeader;

나는 이것을 OnRowDataBound이벤트에 사용합니다 :

protected void GridViewResults_OnRowDataBound(object sender, GridViewRowEventArgs e) {
    if (e.Row.RowType == DataControlRowType.Header) {
        e.Row.TableSection = TableRowSection.TableHeader;
    }
}

답변의 코드는 Page_Load또는 이어야합니다 GridView_PreRender. 나는 그것을 호출 한 메소드에 Page_Load넣고을 얻었다 NullReferenceException.


이 작업을 수행하려면 다음 코드를 사용하십시오.

if내가 추가 진술이 중요합니다.

그렇지 않으면 (그리드를 렌더링하는 방법에 따라) 다음과 같은 예외가 발생합니다.

테이블에는 머리글, 본문 및 바닥 글 순서로 행 섹션이 포함되어야합니다.

protected override void OnPreRender(EventArgs e)
{
    if ( (this.ShowHeader == true && this.Rows.Count > 0)
      || (this.ShowHeaderWhenEmpty == true))
    {
        //Force GridView to use <thead> instead of <tbody> - 11/03/2013 - MCR.
        this.HeaderRow.TableSection = TableRowSection.TableHeader;
    }
    if (this.ShowFooter == true && this.Rows.Count > 0)
    {
        //Force GridView to use <tfoot> instead of <tbody> - 11/03/2013 - MCR.
        this.FooterRow.TableSection = TableRowSection.TableFooter;
    }
    base.OnPreRender(e);
}

this객체 내의 GridView입니다.

실제로 Asp.net GridView를 재정 의하여 내 자신의 사용자 지정 컨트롤을 만들지 만 aspx.cs 페이지에 붙여 넣고 custom-gridview 방식을 사용하는 대신 이름으로 GridView를 참조 할 수 있습니다.

참고 : 바닥 글 논리를 테스트하지는 않았지만 헤더에서 작동한다는 것을 알고 있습니다.


이것은 나를 위해 작동합니다 :

protected void GrdPagosRowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.TableSection = TableRowSection.TableBody;
    }
    else if (e.Row.RowType == DataControlRowType.Header)
    {
        e.Row.TableSection = TableRowSection.TableHeader;
    }
    else if (e.Row.RowType == DataControlRowType.Footer)
    {
        e.Row.TableSection = TableRowSection.TableFooter;
    }
}

이것은 VS2010에서 시도되었습니다.


함수를 만들고 PageLoad이벤트 에서 다음 과 같이 해당 함수를 사용 하십시오.

기능은 다음과 같습니다

private void MakeGridViewPrinterFriendly(GridView gridView) {  
    if (gridView.Rows.Count > 0) {          
        gridView.UseAccessibleHeader = true;  
        gridView.HeaderRow.TableSection = TableRowSection.TableHeader;  
    }  
} 

PageLoad이벤트는 다음과 같습니다

protected void Page_Load(object sender, EventArgs e) {
        if (!IsPostBack)
        {
            MakeGridViewPrinterFriendly(grddata);
        }
}

나는 이것이 오래된 것을 알고 있지만 표준 그리드 뷰에 대한 MikeTeeVee의 답변에 대한 해석은 다음과 같습니다.

aspx 페이지 :

<asp:GridView ID="GridView1" runat="server" 
    OnPreRender="GridView_PreRender">

aspx.cs :

    protected void GridView_PreRender(object sender, EventArgs e)
    {
        GridView gv = (GridView)sender;

        if ((gv.ShowHeader == true && gv.Rows.Count > 0)
            || (gv.ShowHeaderWhenEmpty == true))
        {
            //Force GridView to use <thead> instead of <tbody> - 11/03/2013 - MCR.
            gv.HeaderRow.TableSection = TableRowSection.TableHeader;
        }
        if (gv.ShowFooter == true && gv.Rows.Count > 0)
        {
            //Force GridView to use <tfoot> instead of <tbody> - 11/03/2013 - MCR.
            gv.FooterRow.TableSection = TableRowSection.TableFooter;
        }

    }

jQuery를 사용하여 추가 할 수도 있습니다. 이렇게하면 PostBack에서 삭제되는 TableRowSection.TableHeader의 문제를 피할 수 있습니다.

$('#myTableId').prepend($("<thead></thead>").append($(this).find("#myTableId tr:first")));

참고 URL : https://stackoverflow.com/questions/309101/how-do-i-get-gridview-to-render-thead

반응형