BoundColumn.DataFormatString プロパティとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > BoundColumn.DataFormatString プロパティの意味・解説 

BoundColumn.DataFormatString プロパティ

内の項目の表示形式指定する文字列取得または設定します

名前空間: System.Web.UI.WebControls
アセンブリ: System.Web (system.web.dll 内)
構文構文

Public Overridable Property
 DataFormatString As String
Dim instance As BoundColumn
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)

プロパティ
内の項目の表示形式指定する書式指定文字列既定値は String.Empty です。

解説解説

DataFormatString プロパティ使用して、列内の項目にカスタム書式指定します

データ書式指定文字列は、{ A : Bxx } の書式で、コロン区切られ2 つ部分から構成されます。たとえば、書式指定文字列 {0:F2} を使用すると、2 固定小数点数表示されます。

メモメモ

文字列全体リテラル文字列ではなく書式指定文字列であることを示すために、中かっこ ({}) で囲む必要があります。かっこの外側テキストリテラル テキストとして表示されます。

コロンの前の値 (一般的な例での A) は 0 から始まるパラメータリストパラメータ インデックス指定します

メモメモ

セルには 1 つ値しかないため、この値は必ず 0 に設定します

コロンの後の文字 (一般的な例での B) は値を表示する書式指定します。共通の書式次の表に示します

書式指定文字の後の値 (一般的な例での xx) は、表示する有効桁数または小数点指定します

書式指定文字列詳細については、「書式設定概要」を参照してください

使用例使用例

DataFormatString プロパティ使用して、DataGrid コントロールの各列にさまざまな書式指定する方法次の例に示しますQty 列には整数書式Price 列には通貨書式Weight 列には小数書式、および Expires 列には短い日付と時刻文字列書式それぞれ設定されます。

<%@ Page language="VB" AutoEventWireup="true"
 %>
<%@ Import Namespace="System.Data"
 %>
<%@ Import Namespace="System.Globalization"
 %>

<HTML>
    <HEAD>
        <title>BoundColumn Example</title>
    </HEAD>
    <script runat="server">
  
       ' The Cart and CartView objects temporarily store the data source
 
       ' for the DataGrid control while the page is being processed.
       Dim Cart As DataTable
       Dim CartView As DataView   
 
       Sub Page_Load(sender as Object,
 e As EventArgs) 
 
          ' With a database, use a select query to retrieve the data.
 
          ' Because the data source in this example is an in-memory
 
          ' DataTable, retrieve the data from session state if it exists;
 
          ' otherwise, create the data source.
          GetSource()

          ' The DataGrid control maintains state between posts to the
 
          ' server; therefore it only needs to be bound to a data source
 
          ' the first time the page is loaded or when the data source
 
          ' is updated.
          If Not IsPostBack Then

             BindGrid()

          End If
                   
       End Sub
 
       Sub ItemsGrid_Edit(sender As Object,
 e As DataGridCommandEventArgs) 
 
          ' Set the EditItemIndex property to the index of the item
 
          ' clicked in the DataGrid control to enable editing for that
          ' item. Be sure to rebind the DateGrid to the data source
 
          ' to refresh the control.
          ItemsGrid.EditItemIndex = e.Item.ItemIndex
          BindGrid()
 
       End Sub
  
       Sub ItemsGrid_Cancel(sender As Object,
 e As DataGridCommandEventArgs) 
 
          ' Set the EditItemIndex property to -1 to exit editing mode.
  
          ' Be sure to rebind the DateGrid to the data source to 
          ' refresh the control.
          ItemsGrid.EditItemIndex = -1
          BindGrid()
 
       End Sub
  
       Sub ItemsGrid_Update(sender As Object,
 e As DataGridCommandEventArgs) 
 
          ' Retrieve the updated values.
          ' For bound columns, the edited value is stored in a TextBox.
          ' The TextBox is the 0th control in a cell's Controls collection.
          ' Each cell in the Cells collection of a DataGrid item represents
          ' a column in the DataGrid control.
          Dim updateItem As DataGridItem
             updateItem = e.Item
 
          Dim qtyText As TextBox = CType(e.Item.Cells(2).Controls(0),
 TextBox)
          Dim priceText As TextBox = CType(e.Item.Cells(3).Controls(0),
 TextBox)
 
          Try 
              ' Try to parse the updated values from the input text.
              Dim newQty As Int32
              Dim newPrice As Decimal
             
              newQty = Int32.Parse(qtyText.Text, NumberStyles.Number)
              newPrice = Decimal.Parse(priceText.Text, NumberStyles.Currency)
 
              ' Get the item cell value - "Item 1", "Item
 2", etc.
              ' For read-only columns, the value is stored in the cell
 text.
              Dim item As String
 = updateItem.Cells(1).Text
         
  
              ' With a database, use an update command to update the
 data. Because 
              ' the data source in this example is an in-memory DataTable,
 delete 
              ' the old row and replace it with a new one.
 
              ' Remove the old entry and clear the row filter.
              CartView.RowFilter = "Item='" & item &
 "'"
              If CartView.Count > 0 Then
             
                  CartView.Delete(0)
          
              End If
 
              CartView.RowFilter = ""
  
              ' Add the new entry.
              Dim dr As DataRow
              dr = Cart.NewRow()
 
              dr("Item") = item
              dr("Qty") = newQty
              dr("Price") = newPrice
              dr("Weight") = updateItem.Cells(4).Text
              dr("Expires") = updateItem.Cells(5).Text
              

              Cart.Rows.Add(dr)
          Catch ex As System.FormatException

             ' If parsing the price or quantity caused an 
             ' exception, then leave edit mode without
             ' changing any cell values.
          End Try
 
          ' Set the EditItemIndex property to -1 to exit editing mode.
 
          ' Be sure to rebind the DateGrid to the data source to refresh
          ' the control.
          ItemsGrid.EditItemIndex = -1
          BindGrid()

       End Sub
 
       Sub BindGrid() 
       
          ' Set the data source and bind to the Data Grid control.
          ItemsGrid.DataSource = CartView
          ItemsGrid.DataBind()
 
       End Sub
 
       Sub GetSource()
 
          ' For this example, the data source will be a DataTable that
 is
          ' stored in session state.
          ' If the data source does not exist, create it; otherwise,
 
          ' load the data.
          If Session("ShoppingCart")
 Is Nothing Then 
 
              Cart = New DataTable
              InitSource()
 
          Else
 
             ' Retrieve the sample data from session state.
             Cart = CType(Session("ShoppingCart"),
 DataTable)
 
          End If         
  
          ' Create a DataView and specify the field to sort by.
          CartView = New DataView(Cart)
          CartView.Sort="Item"
 
          Return
 
       End Sub
 
       Sub InitSource()
 
           ' Create the sample data.
  
           ' Define the columns of the table.
            Cart.Columns.Add(New DataColumn("Qty",
 GetType(System.Int32)))
            Cart.Columns.Add(New DataColumn("Item",
 GetType(System.String)))
            Cart.Columns.Add(New DataColumn("Price",
 GetType(System.Decimal)))
            Cart.Columns.Add(New DataColumn("Weight",
 GetType(System.Decimal)))
            Cart.Columns.Add(New DataColumn("Expires",
 GetType(System.DateTime)))

           ' Store the table in session state to persist its values
 
           ' between posts to the server.
           Session("ShoppingCart") = Cart
             
           ' Populate the DataTable with sample data.
           ' The generated table row data look like this:

           Dim dr As DataRow
           Dim i As Integer

           For i = 1 to 4 
          
               dr = Cart.NewRow()
               dr("Qty") = i Mod
 2 + 1
               dr("Item") = "Item
 " + i.ToString()
               dr("Price") = (0.5 * (i + 1))
               dr("Weight") = 5.0
               dr("Expires") = DateTime.Now + TimeSpan.FromDays(7)
               Cart.Rows.Add(dr)

           Next
       End Sub
    </script>
    <body>
        <form runat="server" ID="Form1">
            <h3>BoundColumn DataFormatString Example</h3>
            <asp:DataGrid id="ItemsGrid" 
                BorderColor="black" BorderWidth="1"
 CellPadding="3"
                OnEditCommand="ItemsGrid_Edit" OnCancelCommand="ItemsGrid_Cancel"
 
                OnUpdateCommand="ItemsGrid_Update"
 AutoGenerateColumns="false"
                runat="server" >

                <HeaderStyle BackColor="#aaaadd"></HeaderStyle>

                <Columns>
                    <asp:EditCommandColumn EditText="Edit"
 CancelText="Cancel" 
                         UpdateText="Update" HeaderText="Edit
 Command Column">
                        <ItemStyle Wrap="False"></ItemStyle>
                        <HeaderStyle Wrap="False"></HeaderStyle>
                    </asp:EditCommandColumn>

                    <asp:BoundColumn HeaderText="Item"
 DataField="Item" 
                                     ReadOnly="True"
 />

                    <asp:BoundColumn HeaderText="Quantity"
 DataField="Qty" 
                                     DataFormatString="{0:N0}"/>

                    <asp:BoundColumn HeaderText="Price"
 DataField="Price"
                                     DataFormatString="{0:c}"
 />

                    <asp:BoundColumn HeaderText="Weight"
 DataField="Weight" 
                                     ReadOnly="True"
 DataFormatString="{0:F3}" />

                    <asp:BoundColumn HeaderText="Expires"
 DataField="Expires" 
                                     ReadOnly="True"
 DataFormatString="{0:g}" />

                </Columns>
            </asp:DataGrid>
        </form>
    </body>
</HTML>
<%@ Page language="c#" AutoEventWireup="true"
 %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Globalization" %>

<HTML>
    <HEAD>
        <title>BoundColumn Example</title>
    </HEAD>
       <script runat="server">
        // The Cart and CartView objects temporarily store the data
 source
        // for the DataGrid control while the page is being processed.
        DataTable Cart;
        DataView CartView;   
 
        void Page_Load(Object sender, EventArgs e) 
        {
            // With a database, use a select query to retrieve the data.
 
            // Because the data source in this example is an in-memory
 
            // DataTable, retrieve the data from session state if it
 exists; 
            // otherwise, create the data source.
            GetSource();

            // The DataGrid control maintains state between posts to
 the 
            // server; therefore it only needs to be bound to a data
 source 
            // the first time the page is loaded or when the data source
 
            // is updated.
            if (!IsPostBack)
            {
                BindGrid();
            }
        }
 
        void ItemsGrid_Edit(Object sender, DataGridCommandEventArgs
 e) 
        {
            // Set the EditItemIndex property to the index of the item
 
            // clicked in the DataGrid control to enable editing for
 that
            // item. Be sure to rebind the DateGrid to the data source
 
            // to refresh the control.
            ItemsGrid.EditItemIndex = e.Item.ItemIndex;
            BindGrid();
        }
 
        void ItemsGrid_Cancel(Object sender, DataGridCommandEventArgs
 e) 
        {
            // Set the EditItemIndex property to -1 to exit editing
 mode.  
            // Be sure to rebind the DateGrid to the data source to
 
            // refresh the control.
            ItemsGrid.EditItemIndex = -1;
            BindGrid();
        }
 
        void ItemsGrid_Update(Object sender, DataGridCommandEventArgs
 e) 
        {
            // Retrieve the updated values.
            // For bound columns, the edited value is stored in a TextBox.
            // The TextBox is the 0th control in a cell's Controls collection.
            // Each cell in the Cells collection of a DataGrid item
 represents
            // a column in the DataGrid control.
            DataGridItem updateItem = e.Item;

            TextBox qtyText = (TextBox)updateItem.Cells[2].Controls[0];
            TextBox priceText = (TextBox)updateItem.Cells[3].Controls[0];
            
            try {
                // Try to parse the updated values from the input text.
                Int32 newQty;
                Decimal newPrice;
            
                newQty = Int32.Parse(qtyText.Text, NumberStyles.Number);
                newPrice = Decimal.Parse(priceText.Text, NumberStyles.Currency);

                // Get the item cell value - "Item 1", "Item
 2", etc.
                // For read-only columns, the value is stored in the
 cell text.
                String item = updateItem.Cells[1].Text;
       
                // With a database, use an update command to update
 the data. 
                // Because the data source in this example is an in-memory
                // DataTable, delete the old row and replace it with
 a new one.
    
                // Filter on the updated item, remove it,
                // then clear the row filter.
                CartView.RowFilter = "Item='" + item + "'";
                if (CartView.Count > 0)
                {
                    CartView.Delete(0);
                }
                CartView.RowFilter = "";
 
                // Add the updated entry for the item.
                DataRow dr = Cart.NewRow();
                dr["Item"] = item;
                dr["Qty"] = newQty;
                dr["Price"] = newPrice;
                dr["Weight"] = updateItem.Cells[4].Text;
                dr["Expires"] = updateItem.Cells[5].Text;
            
                Cart.Rows.Add(dr);
            }
            catch (System.FormatException)
            {
                // If parsing the price or quantity caused an 
                // exception, then leave edit mode without
                // changing any cell values.
            }

            // Set the EditItemIndex property to -1 to exit editing
 mode.  
            // Be sure to rebind the DataGrid to the data source to
 refresh
            // the control.
            ItemsGrid.EditItemIndex = -1;
            BindGrid();
        }
 
        void BindGrid() 
        {
            // Set the data source and bind to the Data Grid control.
            ItemsGrid.DataSource = CartView;
            ItemsGrid.DataBind();
        }

        void GetSource()
        {
            // For this example, the data source is a DataTable that
 is 
            // stored in session state.
            // If the data source does not exist, create it; otherwise,
 
            // load the data.
            
            if (Session["ShoppingCart"] == null)
 
            {     
                Cart = new DataTable();
                InitSource();
            } 
            else
            {
                // Retrieve the sample data from session state.
                Cart = (DataTable)Session["ShoppingCart"];
            }         
 
            // Create a DataView and specify the field to sort by.
            CartView = new DataView(Cart);
            CartView.Sort="Item";

            return;
        }
        
        void InitSource()
        {
            // Create the sample data.
 
            // Define the columns of the table.
            Cart.Columns.Add(new DataColumn("Qty", typeof(Int32)));
            Cart.Columns.Add(new DataColumn("Item",
 typeof(String)));
            Cart.Columns.Add(new DataColumn("Price",
 typeof(Decimal)));
            Cart.Columns.Add(new DataColumn("Weight",
 typeof(Decimal)));
            Cart.Columns.Add(new DataColumn("Expires",
 typeof(DateTime)));

            // Store the table in session state to persist its values
 
            // between posts to the server.
            Session["ShoppingCart"] = Cart;
            
            // Populate the DataTable with sample data.
            DataRow dr;  

            for (int i = 1; i <= 4; i++)
 
            {
                dr = Cart.NewRow();
                dr["Qty"] = i % 2 + 1;
                dr["Item"] = "Item " + i.ToString();
                dr["Price"] = (0.50 * (i + 1));
                dr["Weight"] = 5.0;
                dr["Expires"] = DateTime.Now + TimeSpan.FromDays(7);
                Cart.Rows.Add(dr);
            }
        }
        </script>
    <body>
        <form runat="server" ID="Form1">
            <h3>BoundColumn DataFormatString Example</h3>
            <asp:DataGrid id="ItemsGrid" 
                BorderColor="black" BorderWidth="1" CellPadding="3"
                OnEditCommand="ItemsGrid_Edit" OnCancelCommand="ItemsGrid_Cancel"
 
                OnUpdateCommand="ItemsGrid_Update" AutoGenerateColumns="false"
                runat="server" >

                <HeaderStyle BackColor="#aaaadd"></HeaderStyle>

                <Columns>
                    <asp:EditCommandColumn EditText="Edit" CancelText="Cancel"
 
                         UpdateText="Update" HeaderText="Edit Command
 Column">
                        <ItemStyle Wrap="False"></ItemStyle>
                        <HeaderStyle Wrap="False"></HeaderStyle>
                    </asp:EditCommandColumn>

                    <asp:BoundColumn HeaderText="Item" DataField="Item"
 
                                     ReadOnly="True" />

                    <asp:BoundColumn HeaderText="Quantity" DataField="Qty"
 
                                     DataFormatString="{0:N0}"/>

                    <asp:BoundColumn HeaderText="Price" DataField="Price"
                                     DataFormatString="{0:c}" />

                    <asp:BoundColumn HeaderText="Weight" DataField="Weight"
 
                                     ReadOnly="True" DataFormatString="{0:F3}"
 />

                    <asp:BoundColumn HeaderText="Expires" DataField="Expires"
 
                                     ReadOnly="True" DataFormatString="{0:g}"
 />

                </Columns>
            </asp:DataGrid>
        </form>
    </body>
</HTML>
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からBoundColumn.DataFormatString プロパティを検索した結果を表示しています。
Weblioに収録されているすべての辞書からBoundColumn.DataFormatString プロパティを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からBoundColumn.DataFormatString プロパティ を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

BoundColumn.DataFormatString プロパティのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



BoundColumn.DataFormatString プロパティのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS