タグGridViewをレンダリングするコントロールを取得するにはどうすればよいですか? の代わりに配置することは<thead> <tbody>知っていますが、を表示させることはできません。.UseAccessibleHeaders<th><td><thead>
8 に答える
これはそれを行う必要があります:
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);
}
}