如何更改 GridView 中 BoundField 的方向

发布于 2024-08-14 17:59:56 字数 308 浏览 2 评论 0原文

抱歉,如果我的问题看起来很简单。

现在,在我的 GridView 的 BoundField 中,一些数据显示为“12/11/1381”,我想显示为“1381/11/12”(相反方向)。

我知道我可以通过 TemplateField 中的 rtl 和 ltr 更改方向。但由于某种原因,我无法将边界字段转换为模板字段。

我查看了所有 BoundField 属性以改变方向,但我什么也没发现。

我认为应该在 GridView 的 RowDataBound 事件中完成,但我不知道如何!?

您对本案有何看法?
提前致谢。

Sorry if my question seems to simple .

Right now in the BoundField of my GridView some data display like "12/11/1381" that i wanna display like "1381/11/12" (opposite direction) .

I know i could change the direction via rtl and ltr in TemplateField. But because of some reason i can't convert my boundfield to templatefield.

I looked through all BoundField properties in order to change direction , but i found nothing.

I think that should be done in RowDataBound event of GridView , But i don't know how !?

What do you think at this case ?
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不乱于心 2024-08-21 17:59:56

好的,由于它是作为字符串输入的日期,因此至少有两种方法可以做到这一点。

方法1
将字符串数据绑定为 Date 对象而不是 String。由于我现在不知道原始日期的确切日期格式,dd/MM/yyyy 或 MM/dd/yyyy,您必须自己弄清楚。

如果您可以做到这一点,只需将其添加到“BoundField”元素:

DataFormatString="{0:yyyy/MM/dd}" 

DataFormatString="{0:yyyy/dd/MM}" 

方法 2
操作 ROwDataBound oevent 中的文本。我在这里做了一个粗略的,可以完善一下。您可能应该将它们作为 Date 对象进行操作。

在 ASPX 中:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
 onrowdatabound="GridView1_RowDataBound">

代码隐藏:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType != DataControlRowType.DataRow)
        return;
    // assuming the date is at cell index 1
    string[] arr = e.Row.Cells[1].Text.ToString().Split('/');
    e.Row.Cells[1].Text = string.Format("{0}/{1}/{2}", arr[2], arr[1], arr[0]);
}

Ok, since it's a date which was input as string, there are at least 2 ways you can do it.

Method 1
Databind the string as a Date object instead of String. As I do not now the exact date format of the original date, dd/MM/yyyy or MM/dd/yyyy, you'll have to figure it out yourself.

If you can do that, just add this to the "BoundField" element:

DataFormatString="{0:yyyy/MM/dd}" 

or

DataFormatString="{0:yyyy/dd/MM}" 

Method 2
Manipulate the text in ROwDataBound oevent. I've done a crude one here which can be polished up. You should probably manipulate them as Date objects.

In the ASPX:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
 onrowdatabound="GridView1_RowDataBound">

CodeBehind:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType != DataControlRowType.DataRow)
        return;
    // assuming the date is at cell index 1
    string[] arr = e.Row.Cells[1].Text.ToString().Split('/');
    e.Row.Cells[1].Text = string.Format("{0}/{1}/{2}", arr[2], arr[1], arr[0]);
}
野鹿林 2024-08-21 17:59:56

首先确保您已设置 HtmlEncode="False"

尝试将其添加到 BoundField 标记 -

DataFormatString="{0:yyyy/MM/dd}"

这是一篇应该对您有所帮助的好文章 - 如何在 GridView 中设置日期格式

First make sure you have set HtmlEncode="False"

try adding this to the BoundField tag -

DataFormatString="{0:yyyy/MM/dd}"

here is a good article that should help you - How To Set a Date Format In GridView

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文