Reputation: 3641
I have a DateTime
value from SQL Server and I want to deduct 3 minutes from it.
TableA (DateUpdated); -- table in SQL Server
Then I have this code in my C# code-behind file.
lblDateUpdated
is the id of the label for the column DateUpdated
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();
Upvotes: 0
Views: 332
Reputation: 4258
Using @mybirthname's sample. 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();
Upvotes: 1
Reputation: 18127
You should take the label from GridViewRowEventArgs
.
if (e.Row.DataItem == null)
return;
TextBox textBox = e.Row.FindControl("lblDateUpdated") as TextBox;
string value = textBox.Text.Trim();
Upvotes: 2