ButtonField.DataTextFormatString プロパティ
アセンブリ: System.Web (system.web.dll 内)

Dim instance As ButtonField Dim value As String value = instance.DataTextFormatString instance.DataTextFormatString = value
/** @property */ public String get_DataTextFormatString () /** @property */ public void set_DataTextFormatString (String value)
public function get DataTextFormatString () : String public function set DataTextFormatString (value : String)
フィールドの値の表示形式を指定する書式指定文字列。既定値は空の文字列です ("")。この値は、フィールド値に適用される特定の書式が設定されていないことを示します。

DataTextFormatString プロパティを使用すると、ButtonField オブジェクトに表示される値の表示書式を指定できます。DataTextFormatString プロパティを設定しないと、フィールドの値は特定の書式を使用せずに表示されます。
![]() |
---|
書式指定文字列には、任意のリテラル文字列を使用でき、通常はフィールドの値のプレースホルダを含めます。たとえば、書式指定文字列 "Item Value: {0}" が ButtonField オブジェクトに表示されると、{0} プレースホルダはそのフィールドの値に置き換えられます。書式指定文字列の残りの部分は、リテラル テキストとして表示されます。
プレースホルダは、{ A : Bxx } の形式で、中かっこで囲まれた、コロン区切りの 2 つの部分から構成されます。コロンの前の値 (一般的な例での A) には、フィールド値のインデックスを 0 から始まるパラメータ リストの形式で指定します。
![]() |
---|
この A パラメータは、書式指定構文の一部です。各セルはフィールド値を 1 つしか格納できないので、この値は必ず 0 に設定します。 |
コロンおよびコロンの後の値は省略可能です。コロンの後の文字 (一般的な例での B) には、値を表示する際に使用する書式を指定します。共通の書式を次の表に示します。
C | |
D | |
E | |
F | |
G | |
N | |
X |
書式指定文字の後の値 (一般的な例での xx) は、表示する有効桁数または小数点を指定します。たとえば、書式指定文字列 "{0:F2}" を使用すると、2 桁の固定小数点数が表示されます。

DataTextFormatString プロパティを使用して、フィールド値のカスタム表示書式を指定する方法を次のコード例に示します。
<%@ Page language="VB" %> <script runat="server"> Sub AuthorsGridView_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) ' If multiple ButtonField column fields are used, use the ' CommandName property to determine which button was clicked. If e.CommandName = "Select" Then ' Convert the row index stored in the CommandArgument ' property to an Integer. Dim index As Integer = Convert.ToInt32(e.CommandArgument) ' Get the last name of the selected author from the appropriate ' cell in the GridView control. Dim selectedRow As GridViewRow = AuthorsGridView.Rows(index) Dim lastNameCell As TableCell = selectedRow.Cells(1) Dim lastName As String = lastNameCell.Text ' Display the selected author. Message.Text = "You selected " & lastName & "." End If End Sub </script> <html> <body> <form runat="server"> <h3>ButtonField DataTextField Example</h3> <asp:label id="Message" forecolor="Red" runat="server"/> <!-- Set the DataTextField property of the ButtonField --> <!-- declaratively. Set the DataTextFormatString --> <!-- property to apply special formatting to the text. --> <asp:gridview id="AuthorsGridView" datasourceid="AuthorsSqlDataSource" autogeneratecolumns="false" onrowcommand="AuthorsGridView_RowCommand" runat="server"> <columns> <asp:buttonfield buttontype="Link" commandname="Select" headertext="Select Author" datatextfield="au_lname" datatextformatstring="[{0}]" text="Select"/> <asp:boundfield datafield="au_fname" headertext="First Name"/> </columns> </asp:gridview> <!-- This example uses Microsoft SQL Server and connects --> <!-- to the Pubs sample database. --> <asp:sqldatasource id="AuthorsSqlDataSource" selectcommand="SELECT [au_lname], [au_fname] FROM [authors]" connectionstring="server=localhost;database=pubs;integrated security=SSPI" runat="server"> </asp:sqldatasource> </form> </body> </html>
<%@ Page language="C#" %> <script runat="server"> void AuthorsGridView_RowCommand(Object sender, GridViewCommandEventArgs e) { // If multiple ButtonField column fields are used, use the // CommandName property to determine which button was clicked. if(e.CommandName=="Select") { // Convert the row index stored in the CommandArgument // property to an Integer. int index = Convert.ToInt32(e.CommandArgument); // Get the last name of the selected author from the appropriate // cell in the GridView control. GridViewRow selectedRow = AuthorsGridView.Rows[index]; TableCell lastNameCell = selectedRow.Cells[1]; string lastName = lastNameCell.Text; // Display the selected author. Message.Text = "You selected " + lastName + "."; } } </script> <html> <body> <form runat="server"> <h3>ButtonField DataTextField Example</h3> <asp:label id="Message" forecolor="Red" runat="server"/> <!-- Set the DataTextField property of the ButtonField --> <!-- declaratively. Set the DataTextFormatString --> <!-- property to apply special formatting to the text. --> <asp:gridview id="AuthorsGridView" datasourceid="AuthorsSqlDataSource" autogeneratecolumns="false" onrowcommand="AuthorsGridView_RowCommand" runat="server"> <columns> <asp:buttonfield buttontype="Link" commandname="Select" headertext="Select Author" datatextfield="au_lname" datatextformatstring="[{0}]" text="Select"/> <asp:boundfield datafield="au_fname" headertext="First Name"/> </columns> </asp:gridview> <!-- This example uses Microsoft SQL Server and connects --> <!-- to the Pubs sample database. --> <asp:sqldatasource id="AuthorsSqlDataSource" selectcommand="SELECT [au_lname], [au_fname] FROM [authors]" connectionstring="server=localhost;database=pubs;integrated security=SSPI" runat="server"> </asp:sqldatasource> </form> </body> </html>

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- ButtonField.DataTextFormatString プロパティのページへのリンク