如何在网格视图文本框中转换日期时间 [英] How to convert Datetime in grid view text box

查看:106
本文介绍了如何在网格视图文本框中转换日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好伙伴

我需要一些帮助..

我有一个gridview。我拿了文本框,在那个文本框中我显示日期和时间

在gridview的RowUpdating事件中,它不允许我..显示错误,无法将字符串隐式转换为system.DateTime



在SQL表中的字段is-

 StockDate  datetime  



this是我的RowUpdating事件代码 -

 objBusinessUI.StockDate =((TextBox)(GridView1.Rows [e.RowIndex] .Cells [ 3 ]。控件[ 0 ]))。Text.Trim(); 

和i aslo try
objBusinessUI.StockDate = Convert.ToDateTime((TextBox)(GridView1.Rows [e.RowIndex ] .Cells [ 3 ]。控件[ 0 ]))。Text.Trim());



显示运行时错误。



请帮助我..

Thxxx in advance

解决方案

 objBusinessUI.StockDate =(TextBox)GridView1.Rows [e.RowIndex] .FindControl(  txtdate); 



试试这个。


可能是在datetime强制转换时发生此错误。你可以试试



DateTime dt = new DateTime();

dt = DateTime.ParseExact(你的日期是字符串格式,如果是dd / MM / yyyy格式,dd / MM / yyyy,null);

现在使用dt。


从sql中检索数据时使用这个命令



选择转换(varchar(10),Stockdate,103)作为tableName的stdate



它将以dd / mm / yyyy格式返回日期

比您可以轻松地将其绑定到文本框

text ='<%Eval(stdate)%> ;'

Hi guyss
I need some help..
I have a gridview .In that i took text box and in that textbox i am showing date and time
At the RowUpdating event of gridview it doesnt allow me ..its showing error cannot implicitly convert string to system.DateTime

in SQL table the field is-

StockDate datetime


this is my RowUpdating events code-

  objBusinessUI.StockDate =((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.Trim();

and i aslo try 
objBusinessUI.StockDate =Convert.ToDateTime(((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.Trim());


its show runtime error .

Please help me..
Thxxx in advance

解决方案

objBusinessUI.StockDate = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtdate");


try this.


may be this error occurred on datetime casting. you can try as

DateTime dt = new DateTime();
dt = DateTime.ParseExact(your date in string format and if dd/MM/yyyy format, "dd/MM/yyyy", null);
now use dt in future.


When you retrive data from sql you use this command

select convert(varchar(10),Stockdate,103) as stdate from tableName

it will return date in dd/mm/yyyy format
than you can easily bind this to text box
text='<%Eval("stdate")%>'


这篇关于如何在网格视图文本框中转换日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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