Argument out of range exception
-
Greetings experts, I am incredibly frustrated by this task. We have a sorting feature added to our GridView control which works great. Then we decided to adding sorting up and down arrow and that's when we started having issues. I have been working on this now for three days and can't seem to get anywhere with it. I really do need some help. When we run our code, we get keep getting the following error: System.ArgumentOutOfRangeException: 'Specified argument was out of the range of valid values. Parameter name: index' The error is on this line:
string lbText = ((LinkButton)grvSpeakers.HeaderRow.Cells[i].Controls[0]).Text;
I believe this is the relevant code:
protected void OnSorting(object sender, GridViewSortEventArgs e) { string SortDir = string.Empty; DataTable dt = new DataTable(); dt = ViewState\["tables"\] as DataTable; { if (SortDirection == SortDirection.Ascending) { SortDirection = SortDirection.Descending; SortDir = "Desc"; } else { SortDirection = SortDirection.Ascending; SortDir = "Asc"; } DataView sortedView = new DataView(dt); sortedView.Sort = string.Format("{0} {1}", e.SortExpression, SortDir); grvSpeakers.DataSource = sortedView; grvSpeakers.DataBind(); for (int i = 0; i < grvSpeakers.Columns.Count; i++) { string lbText = ((LinkButton)grvSpeakers.HeaderRow.Cells\[i\].Controls\[0\]).Text; if (lbText == e.SortExpression) { TableCell tableCell = grvSpeakers.HeaderRow.Cells\[i\]; System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image(); img.ImageUrl = (SortDir == "Asc") ? "~/Images/down\_arrow.png" : "~/Images/up\_arrow.png"; tableCell.Controls.Add(new LiteralControl(" ")); tableCell.Controls.Add(img); } } } }
Please forgive me for the code dump. I am hoping it helps show what I could be doing wrong. The GridView has 6 header columns with SortExpressions, Speaker Name, Ministry Name, Email, Date Added, Client Name and website URL Each has a sortExpress like
SortExpression="SpeakerName"
for instance. Any assistance is greatly apprecia
-
Greetings experts, I am incredibly frustrated by this task. We have a sorting feature added to our GridView control which works great. Then we decided to adding sorting up and down arrow and that's when we started having issues. I have been working on this now for three days and can't seem to get anywhere with it. I really do need some help. When we run our code, we get keep getting the following error: System.ArgumentOutOfRangeException: 'Specified argument was out of the range of valid values. Parameter name: index' The error is on this line:
string lbText = ((LinkButton)grvSpeakers.HeaderRow.Cells[i].Controls[0]).Text;
I believe this is the relevant code:
protected void OnSorting(object sender, GridViewSortEventArgs e) { string SortDir = string.Empty; DataTable dt = new DataTable(); dt = ViewState\["tables"\] as DataTable; { if (SortDirection == SortDirection.Ascending) { SortDirection = SortDirection.Descending; SortDir = "Desc"; } else { SortDirection = SortDirection.Ascending; SortDir = "Asc"; } DataView sortedView = new DataView(dt); sortedView.Sort = string.Format("{0} {1}", e.SortExpression, SortDir); grvSpeakers.DataSource = sortedView; grvSpeakers.DataBind(); for (int i = 0; i < grvSpeakers.Columns.Count; i++) { string lbText = ((LinkButton)grvSpeakers.HeaderRow.Cells\[i\].Controls\[0\]).Text; if (lbText == e.SortExpression) { TableCell tableCell = grvSpeakers.HeaderRow.Cells\[i\]; System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image(); img.ImageUrl = (SortDir == "Asc") ? "~/Images/down\_arrow.png" : "~/Images/up\_arrow.png"; tableCell.Controls.Add(new LiteralControl(" ")); tableCell.Controls.Add(img); } } } }
Please forgive me for the code dump. I am hoping it helps show what I could be doing wrong. The GridView has 6 header columns with SortExpressions, Speaker Name, Ministry Name, Email, Date Added, Client Name and website URL Each has a sortExpress like
SortExpression="SpeakerName"
for instance. Any assistance is greatly apprecia
string lbText = ((LinkButton)grvSpeakers.HeaderRow.Cells\[i\].Controls\[0\]).Text;
The message suggests that the i index is invalid for the number of cells in the row, or Controls does not contain any items. You need to use the debugger to find out which one is the problem.