Avatar of Mr_Fulano
Mr_Fulano
Flag for United States of America asked on

Linking a TreeView with a DataGridView in C#

Hello, I have a WinForm in Visual Studio C# that contains a TreeView and also a DataGridView. 


I am testing with a file directory path as data and populating each with the DirectoryInfo Class, which allows me to get all directors and all files within a directory. 


I am able to populate both accurately, each representing the content of the file path that I've indicated (  i.e.  string path = "E:\\";  ), which is a USB drive containing Folders and Files. 

 

My question is as follows.... I would like to be able to select (to click on) a TreeNode node and that would automatically select a Row in the DataGridView corresponding to that file in the path.  I would also be great if I could do that in reverse. That being,  select a Row in the DataGridView and have the TreeView highlight its corresponding node (or file). 


In essence, what I am trying to do is LINK the two data container structures together. 

Is there a way to do that...?


Would I have needed to first create an Array and dump the entire file path of "E:\"  into that Array, and then populate each of my containers, those being the TreeView and the DataGridView?


Is there a practical way to do what I'm trying to do and if so, how should I approach this task?  It would seem that there has to be a way, but I'm just not clear on how to approach this.


Thank you in advance for your help.




C#Microsoft Visual Studio

Avatar of undefined
Last Comment
Mr_Fulano

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
it_saige

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Mr_Fulano

ASKER
Hello Saige, first and foremost thank you for your help!  I was able to reproduce your code into a new project, but initially I had 2 problems:

1). I had to move your the OnLoad event to the Form_Load event for the TreeView and DataGridView to populate. I was able to get that working, so that is not an issue any longer (I think).

2). The clicking (selecting) part did not work as it does for you. I would click on either the TreeView or the DataGridView and nothing changed.  However, I then changed the _SelectionChanged event (for the DataGridView and the _NodeMouseClicked events (for the TreeView) and it worked perfectly.

I'm using Visual Studio 2019 C# WinForms, so it may be the version of VS that we're using. That said, you did a MASTERFUL job. THANK YOU!!!

Excellent work and very, very elegantly coded.

it_saige

This is a common practice in Visual Studio regardless of the version.  When you double click on the event you want to associate with a control, Visual Studio will use the control name; e.g. Form, DataGridView and TreeView; etc; etc, postpending _EventName as the naming convention.  If you single click on the event, you can assign your own event name(s).

If you inspect lines 66, 86 and 96 in the Form1.Designer.cs file of my original post, you will see where the association for the event was made with the event handler methods specified.

HTH,

-saige-
Mr_Fulano

ASKER
Hello Saige, I never knew that...!!! WOW, live and learn. Very interesting indeed. Thank you again.

Keep an eye out for a few more questions from me. I may have a few more in the coming days. Thanks again!
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Mr_Fulano

ASKER
Hello Saige, I ran into a small problem with the code above in the way it formats the data.  I posted another question, so you can get additional points, because I think I may not have explained myself enough in my first question.  Please see the question called:    "How to obtain a File System "Structure" in a TreeView and in a DataGridView"

I gave more information about the problem I'm having there....

Thank you.