Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all
I have 2 tables
Department and employees
I want to create one datagridview in VB.NET that shows the departments, and when you click on a department, it will be expanded and displays its employees as the following:

+ Dept 1
+ Dept 2
- Dept 3
Emp 1
Emp 2
Emp 3
+ Dept 4
+ Dept 5

Can you help me please
Posted
Comments
Sergey Alexandrovich Kryukov 28-Jan-12 4:14am    
Are you sure you need to do it based on the grid? How about a regular TreeView? There is one more very attractive solution; please see my answer.
--SA
Sergey Alexandrovich Kryukov 28-Jan-12 13:29pm    
OP commented: Thank you guys, but the task should be build by Microsoft DataGridView, no other control.
--SA
Sergey Alexandrovich Kryukov 28-Jan-12 13:34pm    
You cannot be too lucky with useful answers is you don't motivate things like that. Why "no other", any rational reason?

In this case, this functionality is simply impossible, just because even if you can is the grid, you should derive a class out of it, so formally, this is not the class you want.

But the whole idea is some kind of abuse. If you need some functionality, don't tell us how to do it, expect that someone can tell you that. If you know what to use, you are generally on your own.

You have every right for your "should be... no other", but the whole approach is counter-productive. Don't expect useful much help in this case.

Sorry,
--SA

A while ago I found one control which I liked very much and it could help you. Sorry, it's based not on the grid view but on the list view. Nevertheless, it could be good enough for your purpose. Please see this CodeProject article:

Virtual Mode TreeListView[^].

You will face some problems building this code due to some minor bug. Please see my comment to the article — I explain how to fix them; and the author promised me to improve/fix the code and post a fixed version.

Good luck,
—SA
 
Share this answer
 
Comments
Espen Harlinn 28-Jan-12 13:14pm    
5'ed!
Sergey Alexandrovich Kryukov 28-Jan-12 13:34pm    
Thank you, Espen.
--SA
There's also a control which directly inherits from DataGridView. It's called TreeGridView and is developed by Mark Rideout:

http://blogs.msdn.com/b/markrideout/archive/2006/01/08/customizing-the-datagridview-to-support-expanding-collapsing-ala-treegridview.aspx[^]

Check also the comments on Mark's blog, they contains a few bugfixes and also lots of improvements.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 6-May-13 10:38am    
My 5, thanks for sharing.
—SA
i think u can not do this with fridview !!!!!!
 
Share this answer
 
I have some trick.


Public Class Form1

Dim UpperHeight As Integer
Dim FullHeight As Integer
Dim RowIdx As Integer

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.departmentTableAdapter.Fill(Me.BCMDBDataSet.department)
UpperHeight = Panel1.Height + 3 + DGV1.ColumnHeadersHeight


End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Me.Dispose()
End Sub


Private Sub DGV1_RowHeaderMouseDoubleClick(sender As Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DGV1.RowHeaderMouseDoubleClick
dim i as integer=DGV1.CurrentRow.Cells(0).Value 'dept Number
Me.employeesTableAdapter.FillBydepartment번호(Me.BCMDBDataSet.employees, i)
RowIdx = DGV1.RowTemplate.Height * (DGV1.CurrentRow.Index + 1)
FullHeight = Panel1.Height + DGV1.Height
DGV2.BringToFront()
DGV2.Location = New Point(20, UpperHeight + RowIdx)
DGV2.Size = New Size(DGV1.Width, FullHeight)
End Sub

Private Sub DGV1_SizeChanged(sender As Object, e As System.EventArgs) Handles DGV1.SizeChanged
DGV2.Size = New Size(DGV1.Width, FullHeight)
End Sub

Private Sub DGV2_RowHeaderMouseDoubleClick(sender As Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DGV2.RowHeaderMouseDoubleClick
DGV2.SendToBack()
End Sub
End Class
 
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