Mohammed Atef’s Technical blog

Master Details GridView with Details inside same GridView


I have been worked for implementing Gridview which contain linkbutton in each row that link must be show user details in same row using Detail View Control.


I have developer normal Gridview and did the following steps

1-drag GridView and Detail view control into my page

2- bind Gridview in page load event

3- add 3 template fields and one command select field

4-add labels with unique names in each template field

5-add the following code in GridView RowCommand Event

   1: If e.CommandName = "Select" Then
   2:             Dim userid As String = CType(GridView1.Rows(Integer.Parse(e.CommandArgument)).Cells(0).FindControl("lbluserid"), Label).Text
   3:             DetailsView1.DataSource = GenerateUserDetTable(GenerateUserDetTable(Nothing).Select("userID=" + userid)(0))
   4:             DetailsView1.DataBind()
   5:             GridView1.Rows(Integer.Parse(e.CommandArgument)).Cells.RemoveAt(0)
   6:             GridView1.Rows(Integer.Parse(e.CommandArgument)).Cells.RemoveAt(1)
   7:             GridView1.Rows(Integer.Parse(e.CommandArgument)).Cells(0).Text = ""
   8:             GridView1.Rows(Integer.Parse(e.CommandArgument)).Cells(0).ColumnSpan = 2
   9:             GridView1.Rows(Integer.Parse(e.CommandArgument)).Cells(0).HorizontalAlign = HorizontalAlign.Center
  10:             GridView1.Rows(Integer.Parse(e.CommandArgument)).Cells(0).Controls.Add(DetailsView1)
  11:             Dim lnk As LinkButton
  12:             lnk = New LinkButton
  13:             lnk.CommandName = "Hide"
  14:             lnk.Text = "Hide"
  15:             GridView1.Rows(Integer.Parse(e.CommandArgument)).Cells(1).Text = ""
  16:             GridView1.Rows(Integer.Parse(e.CommandArgument)).Cells(1).Controls.Add(lnk)
  17:         End If

Understanding Code

the whole idea is to remove not needed cells and reset the Column span for the remaining cell and adding detailView into this cell lets see how you can do this

at line 1 i get user is from row that user click show linkbutton there

at lines 5,6 i have removed not needed cells

at lines 8,9,10 i have update remaining cell properties and add Detailview to it

at lines 11 to 16 i have create new linkbutton and replace it with the Show link button

note that GenerateUserDetTable is a method that generating UserDetails Table

you can download the whole project from here

Hope that helped.


September 30, 2009 - Posted by | .Net 2010, C#,VB.Net | , , , , ,

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: