从代码隐藏中为GridTemplateColumn DataBinder.Eval(Container.DataItem)设置货币

时间:2014-04-05 17:46:25

标签: asp.net .net telerik-grid currency

请参阅ASPX页面上的RadGrid的以下代码:

<telerik:GridTemplateColumn DataField="Supplier_Price" Aggregate="Sum" FooterText="Unit Price: " FilterControlAltText="Filter Supplier_Price column" HeaderText="Unit Price" SortExpression="Supplier_Price" UniqueName="Supplier_Price" DataType="System.Double" FooterAggregateFormatString="{0:c}">
    <ItemTemplate>
        <%#DataBinder.Eval(Container.DataItem, "Supplier_Price", "{0:C}")%>
    </ItemTemplate>
    <EditItemTemplate>
        <telerik:RadNumericTextBox runat="server" ID="SupplierPriceRadNumericTextBox" Culture="ar-SA" MinValue="0" MaxValue="9999999999" Text='<%#Bind ("Supplier_Price") %>' Type="Currency" NumberFormat-DecimalSeparator="," NumberFormat-AllowRounding="true" NumberFormat-DecimalDigits="2">
            <NumberFormat DecimalDigits="2" DecimalSeparator="." />
        </telerik:RadNumericTextBox>
    </EditItemTemplate>
</telerik:GridTemplateColumn>

现在我的问题是我需要从后面的代码中更改上面一列中{0:C}的货币文化。

1 个答案:

答案 0 :(得分:0)

我通过将<telerik:GridTemplateColumn>更改为

来解决这个问题
 <telerik:GridNumericColumn DataField="Supplier_Price" Aggregate="Sum" FooterText="Unit Price: " FilterControlAltText="Filter Supplier_Price column" HeaderText="Unit Price" SortExpression="Supplier_Price" UniqueName="Supplier_Price" DataType="System.Double" FooterAggregateFormatString="{0:c}">
 </telerik:GridNumericColumn>

然后在GridNumericColumn中修改代码隐藏中的OnPreRender并添加以下代码:

protected void SupplierPriceRadGrid_PreRender(object sender, EventArgs e)
    {
        foreach (GridColumn column in SupplierPriceRadGrid.Columns)
        {
            if (SelectCurrencyDropDownList.SelectedItem.Value == "SAR")
            {
                if (column.UniqueName == "Supplier_Price")
                {
                    (column as GridNumericColumn).DataFormatString = "{0:N2} .&#1585;.&#1587;";
                    (column as GridNumericColumn).FooterAggregateFormatString = "{0:N2} .&#1585;.&#1587;";
                }
                if (column.UniqueName == "Supplier_Total_Price")
                {
                    (column as GridNumericColumn).DataFormatString = "{0:N2} .&#1585;.&#1587;";
                    (column as GridNumericColumn).FooterAggregateFormatString = "{0:N2} .&#1585;.&#1587;";
                }
            }

            if (SelectCurrencyDropDownList.SelectedItem.Value == "USD")
            {
                if (column.UniqueName == "Supplier_Price")
                {
                    (column as GridNumericColumn).DataFormatString = "$ {0:N2}";
                    (column as GridNumericColumn).FooterAggregateFormatString = "$ {0:N2}";
                }
                if (column.UniqueName == "Supplier_Total_Price")
                {
                    (column as GridNumericColumn).DataFormatString = "$ {0:N2}";
                    (column as GridNumericColumn).FooterAggregateFormatString = "$ {0:N2}";
                }
            }

            if (SelectCurrencyDropDownList.SelectedItem.Value == "EUR")
            {
                if (column.UniqueName == "Supplier_Price")
                {
                    (column as GridNumericColumn).DataFormatString = "€ {0:N2}";
                    (column as GridNumericColumn).FooterAggregateFormatString = "€ {0:N2}";
                }
                if (column.UniqueName == "Supplier_Total_Price")
                {                        
                    (column as GridNumericColumn).DataFormatString = "€ {0:N2}";
                    (column as GridNumericColumn).FooterAggregateFormatString = "€ {0:N2}";
                }
            }

            if (SelectCurrencyDropDownList.SelectedItem.Value == "GBP")
            {
                if (column.UniqueName == "Supplier_Price")
                {
                    (column as GridNumericColumn).DataFormatString = "£ {0:N2}";
                    (column as GridNumericColumn).FooterAggregateFormatString = "£ {0:N2}";
                }
                if (column.UniqueName == "Supplier_Total_Price")
                {
                    (column as GridNumericColumn).DataFormatString = "£ {0:N2}";
                    (column as GridNumericColumn).FooterAggregateFormatString = "£ {0:N2}";
                }
            }

            if (SelectCurrencyDropDownList.SelectedItem.Value == "CNY")
            {
                if (column.UniqueName == "Supplier_Price")
                {
                    (column as GridNumericColumn).DataFormatString = "元 {0:N2}";
                    (column as GridNumericColumn).FooterAggregateFormatString = "元 {0:N2}";
                }
                if (column.UniqueName == "Supplier_Total_Price")
                {
                    (column as GridNumericColumn).DataFormatString = "元 {0:N2}";
                    (column as GridNumericColumn).FooterAggregateFormatString = "元 {0:N2}";
                }
            }

            if (SelectCurrencyDropDownList.SelectedItem.Value == "JPY")
            {
                if (column.UniqueName == "Supplier_Price")
                {
                    (column as GridNumericColumn).DataFormatString = "¥ {0:N2}";
                    (column as GridNumericColumn).FooterAggregateFormatString = "¥ {0:N2}";
                }
                if (column.UniqueName == "Supplier_Total_Price")
                {
                    (column as GridNumericColumn).DataFormatString = "¥ {0:N2}";
                    (column as GridNumericColumn).FooterAggregateFormatString = "¥ {0:N2}";
                }
            }
        }
    }

SelectCurrencyDropDownList.SelectedItem.Value == "JPY"基本上是dropdownlist,用户选择货币,OnPreRender点火,一切正常!