[英]How to add minutes in DateTime value from SQL Server to C#
I have a DateTime
value from SQL Server and I want to deduct 3 minutes from it. 我有一个SQL Server的DateTime
值,我想从中减去3分钟。
TableA (DateUpdated); -- table in SQL Server
Then I have this code in my C# code-behind file. 然后,我的C#代码隐藏文件中就有此代码。
lblDateUpdated
is the id of the label for the column DateUpdated
lblDateUpdated
是列DateUpdated
的标签的ID
protected void grvC_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowIndex == grvC.EditIndex)
{
DateTime dtUpdated = new DateTime();
value2 = ((TextBox)(grvClients.Rows[e.Row.RowIndex].FindControl("lblDateUpdated"))).Text.ToString().Trim();
dtUpdated = Convert.ToDateTime(value2);
dtUpdated = dtUpdated.AddMinutes(-3);
}
}
}
The error it show is this: 它显示的错误是这样的:
Index was out of range. 索引超出范围。 Must be non-negative and less than the size of the collection. 必须为非负数并且小于集合的大小。
on the line 在线上
value2 = ((TextBox)(grvClients.Rows[e.Row.RowIndex].FindControl("lblDateUpdated"))).Text.ToString().Trim();
You should take the label from GridViewRowEventArgs
. 您应该从GridViewRowEventArgs
获取标签。
if (e.Row.DataItem == null)
return;
TextBox textBox = e.Row.FindControl("lblDateUpdated") as TextBox;
string value = textBox.Text.Trim();
Using @mybirthname's sample. 使用@mybirthname的示例。 If its a label, try 如果有标签,请尝试
if (e.Row.DataItem == null)
return;
var lb = e.Row.FindControl("lblDateUpdated") as Label;
var value = ((string)lb.Content).Trim();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.