Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27371

Reading from xml or csv, listbox1 selection populating listbox2 or dgv

$
0
0
Hello all. I'm having some issues deciding on how to setup my project so I can actually begin working on it.

Basically what I want to do is create a program that loads in specific data to a listbox from a csv or xml file. From there the user makes a selection from listbox1 and a second listbox (or datagridview) is populated with data associated to the selection in listbox1.

For example, say I have a list of restaurants and a lot of general information on the restaurant including what they serve (burgers, fries, drinks, tacos), number of employees, name of the restaurant, etc... In listbox1 I want to display all possible things a restaurant serves (burgers, fries, etc...) and based on the user selection listbox2 will display the name of that restaurant. So if the user selects Burgers in listbox1, listbox2 will list all of the restaurant names that have burgers as an item. Here's a sample xml file so you can see the structure:
Code:

<?xml version="1.0" encoding="utf-8" ?>
<restaurants>
  <restaurant>
    <name>name1</name>
    <item>burgers</item>
    <item>fries</item>
    <item>drinks</item>
    <num>12</num>
  </restaurant>
  <restaurant>
    <name>name2</name>
    <item>fries</item>
    <item>tacos</item>
    <item>salads</item>
    <num>35</num>
  </restaurant>
  <restaurant>
    <name>name3</name>
    <item>drinks</item>
    <item>salads</item>
    <item>tacos</item>
    <item>burgers</item>
    <num>27</num>
  </restaurant>
  <restaurant>
    <name>name4</name>
    <item>tacos</item>
    <item>drinks</item>
    <item>salads</item>
    <item>burgers</item>
    <item>quesadillas</item>
    <num>20</num>
  </restaurant>
</restaurants>

I'm all over the place on which direction to go in terms of xml or csv and what will provide more flexibility should things change for certain entries in the future so I haven't even started on the actual programming. I'd just like some direction and general suggestions. I want it to be flexible so if I add more elements or fields things wont get screwed up. I'm thinking xml might be easier to read in the data and pull specific elements but unfortunately since the visual basic book I bought for an intro class wasn't very good I'm not sure about that. It only dedicated a page or so to xml which seemed very odd to me since they claimed it was far better than using csv files. They only provided one type of example of reading from an xml file and nothing on writing back to it if necessary (writing back wont be for this program). I have had some luck using the official microsoft reference but I find it extremely hard to navigate and find what I need.

I am also debating just using a datagridview in case I want to display more than one element/field. Ex: User selects burgers, the dgv displays name of restaurant and a user rating or something.

I'm not sure what keywords to search for with the listbox feature if there is a technical term for that. I did find this topic which seemed to be similar to what I want to do but it's unanswered and fairly old.

Because of my lack of xml experience I'm not very good with it (but I'd like to learn) and despite crawling through the microsoft vb reference pages the code I currently have doesn't seem to be working (trying to populate the dgv to see if I at least got that working, I didn't):
Code:

Public Class Form1
    Dim locationData As XElement = XElement.Load("test.xml")

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim query = From lc In locationData.Descendants("restaurant")
        Let name = lc.<name>.Value
        Let number = lc.<num>.Value
        Order By name Descending
        Select name, number
        dgvOutput.DataSource = query.ToList
        dgvOutput.CurrentCell = Nothing
        dgvOutput.Columns("name").HeaderText = "Name"
        dgvOutput.Columns("number").HeaderText = "Number"
    End Sub
End Class

What am I doing wrong with the query? The header text loads, the actual data does not display. I'm sure I'm missing something stupid, I'll continue to search online (and work on it as well) for xml examples and through my finished vb projects since the book is mostly useless when it comes to doing stuff with xml.

So what I need to do:
-Populate lstSelection with specific data from a file (in this case list all possible items)
-Populate dgvOutput with data associated with selected item (name and number)

Thanks for taking the time to read. Any help, suggestions (and examples), and feedback is appreciated.

Viewing all articles
Browse latest Browse all 27371

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>