WebSurfer's Home

トップ > Blog 1   |   ログイン
APMLフィルター

GridView の列名の取得

by WebSurfer 2010年8月2日 12:35

GridView の n 番目(スタートは n = 0)の列名は、通常、GridView1.HeaderRow.Cells[n].Text のようにして取得できますが、AllowSorting プロパティが true になっていると、それでは取得できません(結果は "" になります)。

何故なら、GridView1.HeaderRow.Cells[n] には LinkButton コントロールが配置され、LinkButton の Text プロパティに列名が設定されるからです。

その場合は、以下のようにして取得できます。

string headerText;
foreach (Control ctl in GridView1.HeaderRow.Cells[n].Controls)
{
  if (ctl is LinkButton)
  {
    headerText = ((LinkButton)ctl).Text; 
    break;
  }
}

たぶん、GridView1.HeaderRow.Cells[n].Controls[0] が LinkButton になると思いますが、保証の限りではないので、ちょっと冗長かもしれませんが、上記のようにループで回して取得するように考えました。

Tags: ,

ASP.NET

About this blog

2010年5月にこのブログを立ち上げました。主に ASP.NET Web アプリ関係の記事です。

Calendar

<<  2024年3月  >>
252627282912
3456789
10111213141516
17181920212223
24252627282930
31123456

View posts in large calendar