Click here to Skip to main content
15,917,991 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi,

This below code which was tranfer object[3] cList to Grid for looping and get assigning.

Please help me to resolve the error.

Error:

foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator'

code:
C#
foreach (GridView gView in cList) {
            CreateWorkSheet(gView.ID.ToString, sw, gView, CellWidth);
        }


tried ways:
C#
foreach (GridView gView in (System.Collections.Generic.IEnumerable<GridView>)cList)
{
}

using this above code i'm getting this error:
Unable to cast object of type 'System.Object[]' to type 'System.Collections.Generic.IEnumerable`1[System.Web.UI.WebControls.GridView]'

C#
foreach (object gViiew in cList)
       {

           GridView gView = (GridView)gViiew;
}
Posted
Updated 7-Sep-12 5:40am
v3
Comments
[no name] 7-Sep-12 10:28am    
Use a variable in your foreach to access the elements in cList or change to a for loop.
visnumca123 7-Sep-12 11:23am    
Hi tried to change var and also same object but its showing same issue

any link it will be useful to debug
[no name] 7-Sep-12 11:27am    
Your statement makes absolutely no sense at all. Why on earth would you try using var? Update your questiojn with what you have tried.
visnumca123 7-Sep-12 11:40am    
please find the updates now
[no name] 7-Sep-12 11:43am    
try
for(int i = 0; i < cList.Count; i++)
CreateWorkSheet(((GridView)cList[i]).ID.ToString, sw, gView, CellWidth);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900