Tuesday, September 30, 2008 3:30 AMGood Day!
I would like to know if it is possible to drag and drop multiple folders containing XML files into a datagrid and have the XML files be read into the datagrid.
currently, I am able to drag and drop multiple files and have them be loaded into the datagrid.
Anyone can help?
Wednesday, October 01, 2008 9:19 AMModeratorHi Kooks,
If you use Dataset(or datatable).WriteXml to save the data into the XML file, then you can use the DataSet(or datatable).ReadXml to fetch the data into the datagridview control.
If XML file isn't generated in the above way, it is structue data. You can use the TreeView control to display this XML file.
Wednesday, October 01, 2008 5:09 PMHi Riquel,
I use the ReadXML. I don't have a problem reading the data.
What I am trying to do is drag and drop folders if that is possible.
The users of the application I have developed have about 300 or more XML files they need to load each time in about 10 seperate folders. I just want to know if I can make it easier on them by giving them the ability to drag and drop the folders and have the files in the folders automatically loaded.
Wednesday, October 01, 2008 6:45 PM
Hi Kareem, hope your well.
I actually saw your post a couple of days ago and I understand where you are coming from and I wanted to create a sample application to see if it possible. What I have done is I opened Notepad to see if I could drop a folder on it and it looked like it worked. Didn't do anything but the drag and drop icon changed to suggest it's possible.
What you could do, as it wouldn't be a big change, is to code up a method that scans a directory and reads in the XML files (it would need to check it was reading in the correct ones) and then drive that method from a user selecting a directory using a folder browser dialog or something similar.
If it is possible to drop folders onto your application then that method (function/sub) would be used. Even if it isn't possible then you have the ability to read in a directory of files anyway.
Thursday, October 02, 2008 12:45 AM
Thank you for your reply.
I tried what you said and it worked, however, being a newbie and a self-learner, I believe my code is quite crude and would probably give any decent programmer a heart attack!!
I know my computer felt like it almost had a heart attack running it!!!
I am adding it here without shame and hope that maybe you can help me clean it up a bit and make it more efficient.
I tested it with a directory containing about 300 XML files in 10 folders and my computer went nuts and i had to stop it. I tried it again with folder containing 30 XML files in 1 folder and it worked. So I know it is operational, just very slow and a processor killer.
Can you help?Code Snippet
PublicClass Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'----- Determine the name and path of the directories, which are located in a specific directory and list them in textbox1
For Each y As String In My.Computer.FileSystem.GetDirectories("C:\Users\Kareem\Desktop\new\")
TextBox1.Text += y & vbCrLf
'----- Determine the name and path of each file located in each path in Textbox1 and list them in Textbox2
For Each x As String In My.Computer.FileSystem.GetFiles(y)
TextBox2.Text += x.ToString & vbCrLf
'----- Extract file name and path from textbox2
For Each s As String In TextBox2.Lines
'----- Read XML file
If s.ToString <> Nothing Then
'---- Display data in Datagrid
DataGridView1.DataSource = DataSet1
DataGridView1.DataMember ="row" End If Next Next Next
Thursday, October 02, 2008 3:50 AMModeratorHi Kareem,
Put For Each s As String In TextBox2.Lines out the above Loop. I think that you execute some repeated operation in this scenario. Second I don't see that you ues the Drag and Drop feature here. Try the following code snippet. If you have any further issues, feel free to tell us.
Public Class Form1
Public Sub New()
AddHandler Me.MouseMove, AddressOf Form1_MouseMove
Me.AllowDrop = True
AddHandler Me.DragDrop, AddressOf Form1_DragDrop
AddHandler Me.DragEnter, AddressOf Form1_DragEnter
Private Sub Form1_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs)
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
Dim files As String() = DirectCast(e.Data.GetData(DataFormats.FileDrop), String())
Catch ex As Exception
Private Sub Form1_DragEnter(ByVal sender As Object, ByVal e As DragEventArgs)
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
e.Effect = DragDropEffects.None
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
System.Console.WriteLine(e.X.ToString() + "," + e.Y.ToString() + "" & Chr(10) & "")
Thursday, October 02, 2008 9:02 PM
Reading in a lot of files will always take a bit of time but I can see a bit of a problem, too many strings. Strings in .NET are immutable which means when you create a string it never changes. When you append one string to another string a third string is created. So when you have a loop of 300 where in the loop you append a string to another string you end up creating 301 strings! Ouch!
You have here three nested loops and each loop does some string appending (concatination).
I've had a rough day so no code improvements but with the above in mind see if your able to improve your code. post it again and I'll review it again no worries.
Riquel has posted the drag and drop code so one thing you want to do is take the code above, that is behind the button click event and make it a sub procedure. Pass a string array to that sub procedure, the directories to read the files from, and then you can use Riquels code to call that sub procedure.
Thursday, October 02, 2008 9:28 PM
I was going to suggest you use listbox instead of textbox. Lists seemed to be more efficient. I recently created a little application to read close to 1000 files in many directories and convert them from word docs to plain text files. I had the listboxes populate the files and status of what was done with the files. Some were removed while others were converted so i wanted to keep track of the progress. But it only took a few minutes to convert all the files. Not sure this will help your situation but lists of some sort might be better. And keep in mind that you don't need to use any controls to work with the filenames. if you want to see them ok but you can work with arrays and lists in the background.
Hope this helps