Click to See Complete Forum and Search --> : Index Out Of Range


kscdavefl
January 28th, 2005, 07:46 AM
When I run the following code:

private void applicationPermissionGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{

if (e.Item.ItemType == System.Web.UI.WebControls.ListItemType.Item ||
(e.Item.ItemType == System.Web.UI.WebControls.ListItemType.AlternatingItem))
{
if(applicationPermissionGrid.Items[e.Item.ItemIndex].Cells[6].Text == "0")
{
applicationPermissionGrid.Items[e.Item.ItemIndex].Cells[0].Text = "NO";
}
}

I get the following error:



Index was out of range. Must be non-negative and less than the size of the collection. 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: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

Source Error:


Line 187: (e.Item.ItemType == System.Web.UI.WebControls.ListItemType.AlternatingItem))
Line 188: {
Line 189: if(applicationPermissionGrid.Items[e.Item.ItemIndex].Cells[6].Text == "0")
Line 190: {
Line 191: applicationPermissionGrid.Items[e.Item.ItemIndex].Cells[0].Text = "NO";


Source File: c:\inetpub\wwwroot\dataviewer\permissionform.aspx.cs Line: 189

Stack Trace:


[ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index]
System.Collections.ArrayList.get_Item(Int32 index) +91
System.Web.UI.WebControls.DataGridItemCollection.get_Item(Int32 index)
DataViewer.permissionForm.applicationPermissionGrid_ItemDataBound(Object sender, DataGridItemEventArgs e) in c:\inetpub\wwwroot\dataviewer\permissionform.aspx.cs:189
System.Web.UI.WebControls.DataGrid.OnItemDataBound(DataGridItemEventArgs e)
System.Web.UI.WebControls.DataGrid.CreateItem(Int32 itemIndex, Int32 dataSourceIndex, ListItemType itemType, Boolean dataBind, Object dataItem, DataGridColumn[] columns, TableRowCollection rows, PagedDataSource pagedDataSource)
System.Web.UI.WebControls.DataGrid.CreateControlHierarchy(Boolean useDataSource)
System.Web.UI.WebControls.BaseDataList.OnDataBinding(EventArgs e)
System.Web.UI.WebControls.BaseDataList.DataBind()
DataViewer.permissionForm.FillApplicationPermissionGrid() in c:\inetpub\wwwroot\dataviewer\permissionform.aspx.cs:156
DataViewer.permissionForm.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\dataviewer\permissionform.aspx.cs:50
System.Web.UI.Control.OnLoad(EventArgs e)
System.Web.UI.Control.LoadRecursive()
System.Web.UI.Page.ProcessRequestMain()

What do I need to change to correct this?

Thanks,

Dave

bewa
January 28th, 2005, 08:07 AM
if(applicationPermissionGrid.Items[e.Item.ItemIndex].Cells[6].Text == "0")

I think Cell[6] is out of bounds. Try with a lower number and see if it works. Remember that indexes are 0 based, so column 6 in a grid is Cells[5]