asp:BoundField 如何将日期和数字从英语翻译成阿拉伯语

How to translate the date and the number from English to Arabic in asp:BoundField

这是我的HTML。我希望 DataGridView 中的日期是阿拉伯语,我想要详细的解决方案,因为我是 asp.net

的初学者
   <asp:GridView ID="GridView1" runat="server" AllowPaging="True" EnableSortingAndPagingCallbacks="false"  ViewStateMode="Enabled" CellPadding="3" 
 AllowSorting="True" GridLines="Vertical" AutoGenerateColumns="False" PageSize="20"  onrowdatabound="GridView1_RowDataBound"
                         DataKeyNames="id" >
        <Columns>
            <asp:BoundField  DataField="stat_date" dataformatstring="{0:dd/MM/yyyy}" HeaderText="التاريخ" ItemStyle-Font Size="Small"/>

我找到了这段代码,但我不知道把它放在哪里,甚至不知道如何在网格视图中使用它

   public string ConvertIntoLocalNumerals(string EnglishValue, string Locale)
   {
       string[] numerals = System.Globalization.CultureInfo.CreateSpecificCulture(Locale).NumberFormat.Nati     veDigits;

        string LocalisedValue = EnglishValue;
        for(int n=0; n < numerals.Length; n++)
        {
            LocalisedValue = LocalisedValue.Replace(n.ToString(), numerals[n]);
        }
        return LocalisedValue;
    }

有两种方法可以做到。一种是您可以创建一个模板字段而不是绑定字段,另一种是您可以在 rowDataBound 方法上进行。

这是模板字段的代码

<asp:TemplateField HeaderText="التاريخ">
                    <ItemTemplate>
                        <%#ConvertIntoLocalNumerals(Eval("stat_date").ToString(),"ar-sa") %>
                    </ItemTemplate>
</asp:TemplateField>

以下是在 RowDataBound 方法中执行此操作的代码

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var arabicDate = ConvertIntoLocalNumerals(DataBinder.Eval(e.Row.DataItem, "start_date").ToString(),"ar-sa");
        e.Row.Cells[0].Text = string.Format("{0:dd/MM/yyyy}", arabicDate);
    }
}

更新

您可以本地化 DateTime 对象而不是字符串,这里是辅助方法。

public string ConvertIntoLocaleDate(DateTime SourceValue, string Locale,string format)
{
    var cultureInfo = CultureInfo.CreateSpecificCulture(Locale);
    cultureInfo.DateTimeFormat.Calendar = new GregorianCalendar();

    string formattedDate = SourceValue.ToString(format, cultureInfo);
    string[] numerals = cultureInfo.NumberFormat.NativeDigits;

    for (int n = 0; n < numerals.Length; n++)
    {
        formattedDate = formattedDate.Replace(n.ToString(), numerals[n]);
    }
    return formattedDate;
}

你的 rowDataBound 代码如下

e.Row.Cells[0].Text = ConvertIntoLocaleDate(Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "start_date")), "ar-sa", "dd/MM/yyyy");