フィールドの値の表示形式を指定する文字列を取得または設定します。
名前空間: System.Web.UI.WebControls
アセンブリ: System.Web (system.web.dll 内)

構文
Public Overridable Property DataFormatString As String
Dim instance As BoundField
Dim value As String
value = instance.DataFormatString
instance.DataFormatString = value
public virtual string DataFormatString { get; set; }
public:
virtual property String^ DataFormatString {
String^ get ();
void set (String^ value);
}
/** @property */
public String get_DataFormatString ()
/** @property */
public void set_DataFormatString (String value)
public function get DataFormatString () : String
public function set DataFormatString (value : String)
プロパティ値
フィールドの値の表示形式を指定する書式指定文字列。既定値は空の文字列 ("") です。このフィールド値に特別な書式指定文字列が適用されていないことを示します。

解説
BoundField オブジェクトに表示する値のカスタムの表示形式を指定するには、DataFormatString プロパティを使用します。DataFormatString プロパティを設定しない場合、フィールド値が特に書式変換されずに表示されます。
メモ : |
|---|
| HtmlEncode プロパティが true の場合、フィールド値が文字列形式に HTML エンコードされてから書式指定文字列が適用されます。日付など、一部のオブジェクトでは、書式指定文字列を使用してオブジェクトを表示する形式を制御することが必要になる場合があります。そのような場合は、HtmlEncode プロパティを false に設定する必要があります。 |
既定では、BoundField オブジェクトを格納するデータ バインド コントロールが読み取り専用モードの場合だけ、書式指定文字列がフィールド値に適用されます。編集モードで書式指定文字列をフィールド値に適用するには、ApplyFormatInEditMode プロパティを true に設定します。
書式指定文字列には、任意のリテラル文字列を使用でき、通常は、フィールド値のプレースホルダが含まれます。たとえば、書式指定文字列 "Item Value: {0}" では、文字列を BoundField オブジェクトに表示するときに、フィールド値が {0} プレースホルダに代入されます。書式指定文字列の残りは、リテラル テキストとして表示されます。
メモ : |
|---|
| 書式指定文字列にプレースホルダが含まれない場合、最終的な表示テキストには、データ ソースから取得したフィールド値は含まれません。 |
プレースホルダは、{A : Bxx} の形式で、中かっこで囲まれた、コロン区切りの 2 つの部分から構成されます。コロンの前の値 (一般的な例での A) は 0 から始まるパラメータのリストのフィールド値インデックスを指定します。
メモ : |
|---|
| このパラメータは、書式指定構文の一部です。各セルには 1 つのフィールド値しかないため、この値は必ず 0 に設定します。 |
コロンおよびコロンの後の値は省略可能です。コロンの後の文字 (一般的な例での B) は値を表示する書式を指定します。共通の書式を次の表に示します。
| 書式指定文字 | 説明 |
| C | 通貨書式で数値を表示します。 |
| D | 10 進数書式で数値を表示します。 |
| E | 科学 (指数) 書式で数値を表示します。 |
| F | 固定書式で数値を表示します。 |
| G | 一般書式で数値を表示します。 |
| N | 数値書式で数値を表示します。 |
| X | 16 進数書式で数値を表示します。 |
メモ : |
|---|
| 書式指定文字では、X を除き大文字と小文字が区別されません。X は大文字または小文字を指定した 16 進数文字を表示します。 |
書式指定文字の後の値 (一般的な例での xx) は、表示する有効桁数または小数点を指定します。たとえば、書式指定文字列 "{0:F2}" を使用すると、2 桁の固定小数点数で表示されます。
書式指定文字列の詳細については、「書式設定の概要」を参照してください。

使用例
DataFormatString プロパティを使用して、フィールドの値のカスタム表示形式を指定するコード例を次に示します。フィールド内の数値は、書式変換され、小数点以下 4 桁が表示されます。
<%@ Page language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head runat="server">
<title>BoundField DataField Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>BoundField DataField Example</h3>
<asp:gridview id="DiscountsGridView"
datasourceid="DiscountsSqlDataSource"
autogeneratecolumns="False"
runat="server">
<columns>
<asp:boundfield datafield="discounttype"
headertext="Discount Type"/>
<asp:boundfield datafield="discount"
dataformatstring="{0:F4}%"
itemstyle-horizontalalign="Right"
headertext="Discount"/>
</columns>
</asp:gridview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Pubs sample database. -->
<asp:sqldatasource id="DiscountsSqlDataSource"
selectcommand="SELECT [discounttype], [stor_id], [lowqty], [highqty], [discount] FROM [discounts]"
connectionstring="<%$ ConnectionStrings:PubsConnectionString%>"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>
<%@ Page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head runat="server">
<title>BoundField DataField Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>BoundField DataField Example</h3>
<asp:gridview id="DiscountsGridView"
datasourceid="DiscountsSqlDataSource"
autogeneratecolumns="False"
runat="server">
<columns>
<asp:boundfield datafield="discounttype"
headertext="Discount Type"/>
<asp:boundfield datafield="discount"
dataformatstring="{0:F4}%"
itemstyle-horizontalalign="Right"
headertext="Discount"/>
</columns>
</asp:gridview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Pubs sample database. -->
<asp:sqldatasource id="DiscountsSqlDataSource"
selectcommand="SELECT [discounttype], [stor_id], [lowqty], [highqty], [discount] FROM [discounts]"
connectionstring="<%$ ConnectionStrings:PubsConnectionString%>"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>

プラットフォーム
Windows 98,Windows Server 2000 SP4,Windows CE,Windows Millennium Edition,Windows Mobile for Pocket PC,Windows Mobile for Smartphone,Windows Server 2003,Windows XP Media Center Edition,Windows XP Professional x64 Edition,Windows XP SP2,Windows XP Starter Edition
Microsoft .NET Framework 3.0 は Windows Vista,Microsoft Windows XP SP2,および Windows Server 2003 SP1 でサポートされています。

バージョン情報
.NET Framework
サポート対象 : 3.0,2.0

参照