ButtonColumn.DataTextFormatString Propriedade

Definição

Obtém ou define a cadeia de caracteres que especifica o formato de exibição da legenda em cada botão.Gets or sets the string that specifies the display format for the caption in each button.

public:
 virtual property System::String ^ DataTextFormatString { System::String ^ get(); void set(System::String ^ value); };
public virtual string DataTextFormatString { get; set; }
member this.DataTextFormatString : string with get, set
Public Overridable Property DataTextFormatString As String

Valor da propriedade

String

A cadeia de caracteres que especifica o formato de exibição da legenda em cada botão.The string that specifies the display format for the caption in each button. O padrão é uma cadeia de caracteres vazia ("").The default is an empty string ("").

Exemplos

O exemplo de código a seguir demonstra como usar a DataTextFormatString propriedade para exibir a legenda de cada botão no ButtonColumn objeto no formato de moeda.The following code example demonstrates how to use the DataTextFormatString property to display the caption of each button in the ButtonColumn object in currency format.

<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
   <script language="c#" runat="server">
 
      ICollection CreateDataSource() 
      {
         DataTable dt = new DataTable();
         DataRow dr;
 
         dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
         dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
         dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));
 
         for (int i = 0; i < 9; i++) 
         {
            dr = dt.NewRow();
 
            dr[0] = i;
            dr[1] = "Item " + i.ToString();
            dr[2] = 1.23 * (i + 1);
 
            dt.Rows.Add(dr);
         }
 
         DataView dv = new DataView(dt);
         return dv;
      }
 
      void Page_Load(Object sender, EventArgs e) 
      {
         if (!IsPostBack) 
         {
            // Load this data only once.
            ItemsGrid.DataSource= CreateDataSource();
            ItemsGrid.DataBind();
         }
      }
 
      void Grid_CartCommand(Object sender, DataGridCommandEventArgs e) 
      {
         
         // e.Item is the table row where the command is raised.
         // For bound columns, the value is stored in the Text property of the TableCell.
         Label1.Text = "You selected: " + e.Item.Cells[0].Text + ".";        
 
      }
 
   </script>
 
<head runat="server">
    <title>ButtonColumn Example</title>
</head>
<body>
 
   <form id="form1" runat="server">
 
      <h3>ButtonColumn Example</h3>
 
 
      <b>Product List</b>
 
      <asp:DataGrid id="ItemsGrid"
           BorderColor="black"
           BorderWidth="1"
           CellPadding="3"
           AutoGenerateColumns="false"
           OnItemCommand="Grid_CartCommand"
           runat="server">

         <HeaderStyle BackColor="#00aaaa">
         </HeaderStyle>
 
         <Columns>
 
            <asp:BoundColumn 
                 HeaderText="Item" 
                 DataField="StringValue"/>

            <asp:ButtonColumn 
                 HeaderText="Price" 
                 ButtonType="PushButton" 
                 DataTextField="CurrencyValue"
                 DataTextFormatString="{0:C}"
                 CommandName="AddToCart" /> 

         </Columns>

      </asp:DataGrid>

      <br /><br />

      <asp:Label id="Label1" runat="server"/>
 
   </form>
 
</body>
</html>

<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
   <script language="vb" runat="server">
        Function CreateDataSource() As ICollection
            Dim dt As New DataTable()
            Dim dr As DataRow
            
            dt.Columns.Add(New DataColumn("IntegerValue", GetType(Int32)))
            dt.Columns.Add(New DataColumn("StringValue", GetType(String)))
            dt.Columns.Add(New DataColumn("CurrencyValue", GetType(Double)))
            
            Dim i As Integer
            For i = 0 To 8
                dr = dt.NewRow()
                dr(0) = i
                dr(1) = "Item " + i.ToString()
                dr(2) = 1.23 *(i + 1)
                dt.Rows.Add(dr)
            Next i
            
            Dim dv As New DataView(dt)
            CreateDataSource = dv
        End Function 'CreateDataSource


        Sub Page_Load(sender As Object, e As EventArgs)
            If Not IsPostBack Then
                ' Load this data only once.
                ItemsGrid.DataSource = CreateDataSource()
                ItemsGrid.DataBind()
            End If
        End Sub 'Page_Load


        Sub Grid_CartCommand(sender As Object, e As DataGridCommandEventArgs)
            
            ' e.Item is the table row where the command is raised.
            ' For bound columns, the value is stored in the Text property of the TableCell.
            Label1.Text = "You selected: " + e.Item.Cells(0).Text + "."
        End Sub 'Grid_CartCommand 
   </script>
<head runat="server">
    <title>ButtonColumn Example</title>
</head>
<body>
 
   <form id="form1" runat="server">
 
      <h3>ButtonColumn Example</h3>
 
 
      <b>Product List</b>
 
      <asp:DataGrid id="ItemsGrid"
           BorderColor="black"
           BorderWidth="1"
           CellPadding="3"
           AutoGenerateColumns="false"
           OnItemCommand="Grid_CartCommand"
           runat="server">

         <HeaderStyle BackColor="#00aaaa">
         </HeaderStyle>
 
         <Columns>
 
            <asp:BoundColumn 
                 HeaderText="Item" 
                 DataField="StringValue"/>

            <asp:ButtonColumn 
                 HeaderText="Price" 
                 ButtonType="PushButton" 
                 DataTextField="CurrencyValue"
                 DataTextFormatString="{0:C}"
                 CommandName="AddToCart" /> 

         </Columns>

      </asp:DataGrid>

      <br /><br />

      <asp:Label id="Label1" runat="server"/>
 
   </form>
 
</body>
</html>

Comentários

Use a DataTextFormatString propriedade para fornecer um formato de exibição personalizado para a legenda dos botões no ButtonColumn objeto.Use the DataTextFormatString property to provide a custom display format for the caption of the buttons in the ButtonColumn object.

A cadeia de caracteres de formato de dados consiste em duas partes, separadas por dois-pontos, no formato {A:Bxx} .The data format string consists of two parts, separated by a colon, in the form {A:Bxx}. Por exemplo, a cadeia de caracteres de formatação {0:F2} formata a célula para exibir um número de ponto fixo com duas casas decimais.For example, the formatting string {0:F2} formats the cell to display a fixed point number with two decimal places.

Observação

A cadeia de caracteres inteira deve ser colocada entre chaves para indicar que é uma cadeia de caracteres de formato e não uma cadeia de caracteres literal.The entire string must be enclosed in braces to indicate that it is a format string and not a literal string. Qualquer texto fora das chaves é exibido como texto literal.Any text outside the braces is displayed as literal text.

O valor antes dos dois-pontos ( A no exemplo geral) especifica o índice de parâmetro em uma lista de parâmetros com base em zero.The value before the colon (A in the general example) specifies the parameter index in a zero-based list of parameters.

Observação

Esse valor só pode ser definido como 0 porque há apenas um valor em cada célula.This value can be set only to 0 because there is only one value in each cell.

O caractere após os dois-pontos ( B no exemplo geral) especifica o formato no qual exibir o valor.The character after the colon (B in the general example) specifies the format to display the value in. A tabela a seguir lista os formatos comuns.The following table lists the common formats.

Formatar caractereFormat character Exibe valores numéricos emDisplays numeric values in
C Formato de moeda.Currency format.
D Formato decimal.Decimal format.
E Formato científico (exponencial).Scientific (exponential) format.
F Formato fixo.Fixed format.
G Formato geral.General format.
N Formato de número.Number format.
X Formato hexadecimal.Hexadecimal format.

Observação

O caractere de formato não diferencia maiúsculas de minúsculas, exceto para X , que exibe os caracteres hexadecimais no caso especificado.The format character is not case sensitive, except for X, which displays the hexadecimal characters in the case specified.

O valor após o caractere de formato ( xx no exemplo geral) especifica o número de dígitos significativos ou casas decimais a serem exibidos.The value after the format character (xx in the general example) specifies the number of significant digits or decimal places to display.

O valor dessa propriedade é armazenado em estado de exibição.The value of this property is stored in view state.

Para mais informações sobre cadeias de caracteres de formatação, consulte Tipos de formatação.For more information on formatting strings, see Formatting Types.

Aplica-se a

Confira também