如何在Gridview编辑项模板中以Dd / Mm / Yyyy格式获取特定日期 [英] How Do I Obtain A Specific Date In Dd/Mm/Yyyy Format Inside Gridview Edit Item Template

查看:74
本文介绍了如何在Gridview编辑项模板中以Dd / Mm / Yyyy格式获取特定日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gridview,包含来自数据库的日期列。我用数据库成功绑定它们,我以dd / MM / yyyy格式获取日期但是当我点击自动生成的编辑按钮然后我以dd / MM / yyyy格式获取日期但是当我点击更新按钮然后它给出了错误

字符串未被识别为有效的DateTime。



在gridview行中我有两个不同的日期如下



表格日期 - 2014年9月11日

迄今为止 - 15/11/2014



我在更新日期时得到了上述错误以便更新日期



这是我的GridView代码



 <   asp:TemplateField     HeaderText   =  From Date >  
< EditItemTemplate >
< asp:TextBox ID < span class =code-keyword> =
from_date runat = server 文字 =' <% #Bind( from_date,< span class =code-string> {0:dd / MM / yyyy}% > ' > < / asp:TextBox >
< / EditItemTemplate >
< ; ItemTemplate >
< asp:标签 ID = Label2 runat = server 文字 =' <% #Bind( from_date {0:dd / MM / yyyy}%> ' > < / asp:Label >
< / ItemTemplate > ;
< / asp:TemplateField >
< asp:TemplateField HeaderText = 迄今为止 >
< EditItemTemplate >
< < span class =code-leadattribute> asp:TextBox ID = to_date runat = server 文本 =' <% #Bind( to_date {0:dd / MM / yyyy}%> ' > < / asp:TextBox >
< < span class =code-leadattribute> / EditItemTemplate >
< ItemTemplate >
< asp:标签 ID = Label3 runat = server 文本 =' <% #Bind( to_date {0:dd / MM / yyyy}%> ' > < / asp:标签 >
< / ItemTemplate >
< / asp:TemplateField >



更新点击我有这样的代码

  protected   void  leave_gridview_RowUpdating( object  sender,GridViewUpdateEventArgs e)
{
GridViewRow row =(GridViewRow)leave_gridview.Rows [e.RowIndex];
标签leave_id =(标签)row.FindControl( leave_id);
string num = leave_id.Text;
TextBox from_date =(TextBox)row.FindControl( from_date);
DateTime from_dt = Convert.ToDateTime(from_date.Text);
TextBox to_date =(TextBox)row.FindControl( to_date);
DateTime to_dt = Convert.ToDateTime(to_date.Text);
TextBox sick_leaves =(TextBox)row.FindControl( sick_leaves_allowed);
int new_sick_leaves = Convert.ToInt32(sick_leaves.Text);
TextBox casual_leaves =(TextBox)row.FindControl( casual_leaves_allowed);
int new_casual_leaves = Convert.ToInt32(casual_leaves.Text);
}







请帮我解决这个错误

感谢大家一直支持我

解决方案

转换字符串时需要使用格式。

选项。选择一名获胜者。



如果您的日期格式如此dd / MM / yyyy,请使用此代码

 DateTime from_dt = DateTime.ParseExact(from_date.Text,  dd / MM / yyyy null ); 



如果您的日期格式为MM / dd / yyyy,请使用此代码

 DateTime from_dt = DateTime.ParseExact(from_date.Text,  MM / dd / yyyy null ); 



如果您的日期格式看起来像这样yyyy-MM-dd然后使用这段代码

 DateTime from_dt = DateTime.ParseExact(from_date.Text,  yyyy-MM-dd null ); 







更多信息请参考这些链接

标准日期和时间格式字符串 [ ^ ]



< a href =http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx>自定义日期和时间格式字符串 [ ^ ]

有关详细信息,请参阅这些链接

标准日期和时间格式字符串 [ ^ ]



自定义日期和时间格式字符串 [ ^ ]


I have a gridview with containing to date columns from database. I bind them successfully with database and i obtain the dates in dd/MM/yyyy format but when i clicked on auto generated Edit Button then i obtain the dates in dd/MM/yyyy format but when i clicked on update button then it gives the error that
String was not recognized as a valid DateTime.

In gridview row i have two different date such as follows

Form Date - 11/09/2014
To Date - 15/11/2014

I got the above mentioned error for to date conversion while updating date

Here is my code for GridView

<asp:TemplateField HeaderText="From Date">
        <EditItemTemplate>
            <asp:TextBox ID="from_date" runat="server" Text='<%# Bind("from_date","{0:dd/MM/yyyy}") %>'></asp:TextBox>
        </EditItemTemplate>
        <ItemTemplate>
            <asp:Label ID="Label2" runat="server" Text='<%# Bind("from_date","{0:dd/MM/yyyy}") %>'></asp:Label>
        </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="To Date">
        <EditItemTemplate>
            <asp:TextBox ID="to_date" runat="server" Text='<%# Bind("to_date","{0:dd/MM/yyyy}") %>'></asp:TextBox>
        </EditItemTemplate>
        <ItemTemplate>
            <asp:Label ID="Label3" runat="server" Text='<%# Bind("to_date","{0:dd/MM/yyyy}") %>'></asp:Label>
        </ItemTemplate>
</asp:TemplateField>


on update click i have a code like this

protected void leave_gridview_RowUpdating(object sender, GridViewUpdateEventArgs e)
   {
       GridViewRow row = (GridViewRow)leave_gridview.Rows[e.RowIndex];
       Label leave_id = (Label)row.FindControl("leave_id");
       string num = leave_id.Text;
       TextBox from_date = (TextBox)row.FindControl("from_date");
       DateTime from_dt = Convert.ToDateTime(from_date.Text);
       TextBox to_date = (TextBox)row.FindControl("to_date");
       DateTime to_dt = Convert.ToDateTime(to_date.Text);
       TextBox sick_leaves = (TextBox)row.FindControl("sick_leaves_allowed");
       int new_sick_leaves = Convert.ToInt32(sick_leaves.Text);
       TextBox casual_leaves = (TextBox)row.FindControl("casual_leaves_allowed");
       int new_casual_leaves = Convert.ToInt32(casual_leaves.Text);
   }




Please help me to solve my this error
Thanks to you all for always supporting me

解决方案

You need to use formatting when converting the string.
Some options. Pick a winner.

If your date format looks like this "dd/MM/yyyy" then use this code

DateTime from_dt = DateTime.ParseExact(from_date.Text, "dd/MM/yyyy", null);


If your date format looks like this "MM/dd/yyyy" then use this code

DateTime from_dt = DateTime.ParseExact(from_date.Text, "MM/dd/yyyy", null);


If your date format looks like this "yyyy-MM-dd" then use this code

DateTime from_dt = DateTime.ParseExact(from_date.Text, "yyyy-MM-dd", null);




Refer to these links for more info
Standard Date and Time Format Strings[^]

Custom Date and Time Format Strings[^]
Refer to these links for more info
Standard Date and Time Format Strings[^]

Custom Date and Time Format Strings[^]


这篇关于如何在Gridview编辑项模板中以Dd / Mm / Yyyy格式获取特定日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆