Answered by:
ASP.NET when add datetime color i get Error

Question
-
User-1683022223 posted
The index is out of range. Its value should not be negative and should not be less than the size of the group.
Parameter name: indexHello...
the project work very well but when i try press button edit i get this error
when i add the code color time about PM/AM change color i get error
without this code color datetime work very well
i get this error :
Line 433:
Server Error in '/' Application. The index is out of range. Its value should not be negative and should not be less than the size of the group. Parameter name: index Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.ArgumentOutOfRangeException: الفهرس خارج النطاق. يجب ألا يكون قيمته سالبة ويجب ألا يكون أقل من حجم المجموعة. Parameter name: index Source Error: Line 431: { Line 432: int rowIndex = Convert.ToInt32(e.CommandArgument); Line 433: GridViewRow row = gvFiles.Rows[rowIndex]; Line 434: Line 435: txtSearch.Text = (row.FindControl("lblidcivilv") as Label).Text; Source File: C:\Users\kanko\OneDrive\gsc\gsc\Admin\Vacations.aspx.cs Line: 433
my code
protected void OnRowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex != gvFiles.EditIndex) { DateTime time = Convert.ToDateTime((e.Row.FindControl("Labeldateadd") as Label).Text); if (time.ToString("tt").ToUpper() == "PM") { (e.Row.FindControl("Labeldateadd") as Label).ForeColor = Color.Red; } if (time.ToString("tt").ToUpper() == "م") { (e.Row.FindControl("Labeldateadd") as Label).ForeColor = Color.Red; } if (time.ToString("tt").ToUpper() == "AM") { (e.Row.FindControl("Labeldateadd") as Label).ForeColor = Color.Blue; } if (time.ToString("tt").ToUpper() == "ص") { (e.Row.FindControl("Labeldateadd") as Label).ForeColor = Color.Blue; } string idnamev = (e.Row.FindControl("lblidnamev") as Label).Text; string datestart = Convert.ToDateTime((e.Row.FindControl("lbldate2") as Label).Text).ToString("dd/MM/yyyy"); string dateend = Convert.ToDateTime((e.Row.FindControl("lbltime") as Label).Text).ToString("dd/MM/yyyy"); foreach (LinkButton button in e.Row.Cells[12].Controls.OfType<LinkButton>()) { if (button.CommandName == "Delete") { button.Attributes["onclick"] = "if(!confirm('Do you want to delet\\nأسم الموظف\\n" + idnamev + "\\nبداية الاحازة - نهاية الاجازة\\n" + datestart + " - " + dateend + "')){ return false; };"; } } } }
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { int rowIndex = Convert.ToInt32(e.CommandArgument); GridViewRow row = gvFiles.Rows[rowIndex]; txtSearch.Text = (row.FindControl("lblidcivilv") as Label).Text; }
Tuesday, January 19, 2021 5:00 PM
Answers
-
User1535942433 posted
Hi kankonq8,
Accroding to your codes and problem,do you have set command name?You could post your aspx page to us.
I have created a test and it works fine.I think you loss command name.You could do like this:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:buttonfield buttontype="Link"
commandname="Add"
text="Add"/>
</Columns>
</asp:GridView>Code-behind:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Add") { int rowIndex = Convert.ToInt32(e.CommandArgument); GridViewRow row = GridView1.Rows[rowIndex]; } }
Best regards,
Yijing Sun
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, January 20, 2021 6:00 AM
All replies
-
User-1545767719 posted
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { int rowIndex = Convert.ToInt32(e.CommandArgument); GridViewRow row = gvFiles.Rows[rowIndex]; txtSearch.Text = (row.FindControl("lblidcivilv") as Label).Text; }
You are only one who can debug the actual code on the Visual Studio. So, why don’t you use the debugger?
Put a breakpoint on first line in the GridView1_RowCommand method, click "Step Over" button on the menu bar, and see if value of the rowIndex is within the range of gvFiles.Rows.
Wednesday, January 20, 2021 1:59 AM -
User1535942433 posted
Hi kankonq8,
Accroding to your codes and problem,do you have set command name?You could post your aspx page to us.
I have created a test and it works fine.I think you loss command name.You could do like this:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:buttonfield buttontype="Link"
commandname="Add"
text="Add"/>
</Columns>
</asp:GridView>Code-behind:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Add") { int rowIndex = Convert.ToInt32(e.CommandArgument); GridViewRow row = GridView1.Rows[rowIndex]; } }
Best regards,
Yijing Sun
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, January 20, 2021 6:00 AM