Click here to Skip to main content
15,888,081 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
i have problem to find label control value in method

code here
C#
private void grdProjectBind()
        {
            int userClientID = 0;
            Project ObjProject = new Project();
            try
            {

                foreach (GridView row in grdClient.Rows)
                {
                     userClientID = Convert.ToInt32(((Label)row.FindControl("lbUserClientID")).Text);//error here
                
                }

                   // int userClientID = Convert.ToInt32(((Label) .FindControl("lbUserClientID")).Text);


                ObjProject.UserClientID = userClientID;

                    DataSet ds = new DataSet();
                    ds = ObjProject.GetProjectList();
                    if (ds.Tables[0].Rows.Count != 0)
                    {
                        grdProject.DataSource = ds;
                        grdProject.DataBind();
                        btnAddProject.Visible = false;
                    }
                    else
                    {
                        grdProject.DataBind();
                        btnAddProject.Visible = true;

                    }
                }
            
            catch (Exception)
            {

                throw;
            }
            finally
            {
                ObjProject = null;
            }
        
        
        }


Explaination of error: Unable to cast object of type 'System.Web.UI.WebControls.GridViewRow' to type 'System.Web.UI.WebControls.GridView'.


thanks
Posted
Updated 7-Mar-12 3:07am
v2
Comments
Herman<T>.Instance 7-Mar-12 9:09am    
have you tried
foreach (GridView row in grdClient.Rows)
{
grdClients.SelectedIndex = row.RowIndex;
userClientID = Convert.ToInt32(((Label)grdClients.SelectedRow.FindControl("lbUserClientID")).Text);//error here
grdClients.SelectedIndex = -1;
}

Hi,

Your exception is telling you whats wrong.
You need to use GridViewRow Not GridView in for loop: foreach (GridViewRow row in grdClient.Rows)

grdClient.Rows is a collection of GridViewRow objects. Check here[^].
 
Share this answer
 
Comments
Wonde Tadesse 7-Mar-12 22:34pm    
5+
foreach (GridView row in grdClient.Rows)


Check it:
foreach (GridViewRow row in grdClient.Rows)
 
Share this answer
 

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