Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I would like to show another GridView when I click any row of first GridView.
I have problem to find out index of particular row at selected index event of first GridView.

In the first GridView I have client names and in the second GridView I have project name according to the client.
Like client1 has 2 projects and client2 has 5 projects.

I would like when I click on first row of first GridView, meaning click on client1 2 projects show in the second GridView.
I am Working in ASP.NET Web Application.

Thanks..
Here is the code:
C#
protected void grdClient_SelectedIndexChanged(object sender, EventArgs e)
      {
          Project ObjProject = new Project();
          int userClientID;
          userClientID = Convert.ToInt32(((Label)(grdClient.FindControl("lbUserClientID"))).Text);// Object reference not set to an instance of an object.
          ObjProject.UserClientID = userClientID;

          grdProject.DataSource = ObjProject.GetProjectList();
          grdProject.DataBind();
      }

I am unable to typecast label value at SelectedIndex event.
Posted
Updated 4-Mar-12 20:56pm
v6
Comments
Shahin Khorshidnia 5-Mar-12 2:26am    
And GridView in what? ASP.Net or a WinForm or WPF?
Please tag it then the experts can help you better.
nawabprince 5-Mar-12 2:29am    
okk sir..
André Kraak 5-Mar-12 2:56am    
Edited question:
Added pre tags
Formatted text/code
Spelling/Grammar
Sergey Alexandrovich Kryukov 5-Mar-12 3:22am    
What is supposed to mean: "call a gridview"? It's now a function, method or procedure, not a property.
--SA
nawabprince 5-Mar-12 4:33am    
call a gridview means..bind another girdview with respect to one gridview..means one gridview have clients..and another girdview have project with respect to clients..

GridViewRow row = ItemsGridView.SelectedRow


Project ObjProject = new Project();
            int userClientID;
            userClientID = Convert.ToInt32(((Label)(row.Cells[0].FindControl("lbUserClientID"))).Text);// Object reference not set to an instance of an object.
            ObjProject.UserClientID = userClientID;
 
            grdProject.DataSource = ObjProject.GetProjectList();
            grdProject.DataBind();
 
Share this answer
 
Why don't you use SelectedValue property?
If your gridView has DataKeyNames property set you don't have to use such long way to retrieve desired value.

C#
protected void grdClient_SelectedIndexChanged(object sender, EventArgs e)
{
    if(grdClient.SelectedValue != null)
    {
       Project ObjProject = new Project();
       int userClientID;
       userClientID = int.Parse(grdClient.SelectedValue.ToString());
       ObjProject.UserClientID = userClientID;

      grdProject.DataSource = ObjProject.GetProjectList();
      grdProject.DataBind();
    }
}
 
Share this answer
 
Comments
nawabprince 5-Mar-12 4:31am    
Thanks Oshtri..its working fine..bt now i have problem when i click on edit or delete button .it fired selectedindex event...bt i need only fire clientsname and it call another girdview 2 which have projects w.r.t clients..
Oshtri Deka 6-Mar-12 7:33am    
I am trying to understand what is your problem, your comment is hard to read.
Please use editing option and make it more readable.

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