DataGridView Like Microsoft Access Child/Detail

daPoet

Member
Joined
Jul 22, 2008
Messages
23
Programming Experience
Beginner
How do I create a Master Detail Form with DataGridView

For anyone else looking for a way to use DataGridView in the same way it would appear in Microsoft Access, This is what I've come up with just as a guide..


From what I've been reading, the closest thing that can be used in .NET to do this is ASP.NET since its more likely to be done as a web component..

So what I'm going to do is this. Using One DataGridView on my Main Form, I'm going to use code to resize the Row Size when I've clicked on a particular row. i.e. Make the Row Get Taller/Longer,


Then use code to place another DataGridView, "the child" inside of a panel that is minimised and hidden on my Master Form.
This Child DataGridView inside of the Second Panel, will then be placed withing the Space of the Heightened Row that was selected..

I'll post the Code fragments when I'm done so far Im gona test this code for the row resizing
VB.NET:
 Private Sub DataGridView1_CellPainting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
        If e.RowIndex > -1 Then
            DataGridView1.Rows(e.RowIndex).Height = 35
        End If
End Sub
 
VB.NET:
   Private Sub dataGridView1_RowHeaderMouseClick(ByVal sender As Object, ByVal e As DataGridViewCellMouseEventArgs) Handles Tbl_DptDataGridView.RowHeaderMouseClick
        Me.Tbl_DptDataGridView.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect
        If Me.Tbl_DptDataGridView.Rows(e.RowIndex).Selected = True Then
            Tbl_DptDataGridView.Rows(e.RowIndex).Height = 80
        End If
    End Sub

This code resizes the row height when that row is selected. ;-)
1. The next part of the problem is to return its size to the default when another cell is selected
2. Also when the row's height increase I need to get the contents to be positioned at the top of that cell

When I've solved, I'll post
 
Okay, so of the last two problems I've said I had, this is what I've done to correct the formatting of the cell contents in the cell problem


I used this code
VB.NET:
Me.Tbl_DptDataGridView.Rows(e.RowIndex).DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter

So at least I dont have to worry that the content's of the row will be hidden behind my second panel with the child data grid when I place it in that row space
 
Okay so the code may need tiding up, maybe someparts in the If syntax may even be redundant but this is what I have and so far it works like a charm for what I wana do.

Im gona do this with a 4 tier table link up structure, so the first Grid is going to be in the biggest panel, the second in the second biggest, the third in the third biggest etc

The first grid-row will see the second grid appearing as though it is "nested" within the expanded row etc

You will need to modify the code to get the panel to dynamically appear lower and lower depending on which row is selected as you go down the records in your Grid,

OR

You will need to autoscroll the selected record you've clicked in the parent grid to the Top of that Grid, that way you wouldnt need to move the Child grids ,,,

Hope this helps someone

I'll post the final thing with an example when its totally done, but again, I still dont know how to export a project lol

VB.NET:
Private Sub dataGridView1_RowHeaderMouseClick(ByVal sender As Object, ByVal e As DataGridViewCellMouseEventArgs) Handles Tbl_DptDataGridView.RowHeaderMouseClick
        Me.Tbl_DptDataGridView.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect
        Me.txt_Flag.Text = ""
        If Me.Tbl_DptDataGridView.Rows(e.RowIndex).Selected = True Then
            If Me.Tbl_DptDataGridView.Rows(e.RowIndex).Height <> 300 Then
                Tbl_DptDataGridView.Rows(e.RowIndex).Height = 300
                Me.Tbl_DptDataGridView.Rows(e.RowIndex).DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter
                Me.Panel2.Location = New Point((Panel1.Location.X + 50), (Panel1.Location.Y + 50))
                Me.Panel2.Visible = True
            Else
                Tbl_DptDataGridView.Rows(e.RowIndex).Height = 20
                Me.Panel2.Visible = False
            End If
        Else

        End If


    End Sub
 
Back
Top