Listbox1 and listbox2 to datagridview

Superfurry

Member
Joined
Feb 8, 2011
Messages
19
Programming Experience
1-3
Hello everyone I wander if you could help me

I have two listboxes with data in , I would like to play them both into a datagridview

My datagridview has two columns , I would like to place the data in listbox1 into Column 1 and listboxes 2 into column 2 .

I have tried and try but can not work it out ? Any help would be much appreciated

Thanks
 
Where did the data comes from? Given that the user can't enter data directly into a ListBox, it must have come from somewhere else. It would make more sense to combine it before putting into the ListBoxes, rather than putting it in, then getting it out and combining it and putting it somewhere else.
 
Hi thank you for the reply .

Well it comes one page of a website . I couldn't work out how to grab both sets and put it straight into the Datagridview so I did it separately. If I combine it how would I then split it up into two columns in the Datagridview?
 
Well it comes one page of a website . I couldn't work out how to grab both sets and put it straight into the Datagridview so I did it separately.
So it sounds like you don't actually need the ListBoxes at all then. You don't really want the user to see them at all, right?
If I combine it how would I then split it up into two columns in the Datagridview?
You don't have to split it up. Combining it means putting it al into a single data structure but that's not all in one value. For instance, you might put the data into a DataTable and you can then bind that to the grid in one line. You could also define a type that represented one record and create a collection of instances of that type, once again, bind that.

How exactly are you reading the data currently and what's the relationship between the two items of data fro a single record?
 
Umm I'm reading them from a website using a separate for loop for each one , using getelementbyclass and then looping to get the href. But I have to do it twice to get each one ?
 
But I have to do it twice to get each one ?

You put a question mark there but I don't know whether that's supposed to be a question or a statement. If it's a question then it's one that I can't answer because I have no idea what you're actually reading. Perhaps if you were to provide the code then we would know what you're actually doing and whether it can be done better.

Regardless, there's still no need to use ListBoxes. Think about it. If you can loop through your elements and add values to the Items collection of a ListBox then why can't you add them to some other collection that doesn't require a control? In fact, why can't you add them directly to the grid, first one column in the first loop and then another in the second? My choice would be to add the to a generic List of items that have two properties. You populate the first property in the first loop and add the new items to the list and then you populate the second property of the existing items in the second loop. Once you're done, simply bind the list to the grid.
 
Yes you are right I should add them straight to the Datagridview , I can get the first list into the first column but I don't know how to add the second list into the second column with out it going into the first one ? Sorry I hope that explains it better . I'm at work now so I can't give the code at present :/ I hope that gives you a better idea . I use datagridview1.items.add(cur.tostring) I think that is what I used for the first one
 
I use datagridview1.items.add(cur.tostring) I think that is what I used for the first one

You would be using something like:
VB.NET:
DataGridView1.Rows.Add(cur.ToString())
As that suggests, it adds a row to the grid. Once you're done with the first loop, you've added all the rows you want to add so you wouldn't use Rows.Add. What you want to do in the second loop is set a cell value in an existing row. The easiest way to do that would be using the Item property of the grid, you simply pass it the column index, which won't change, and the row index, which you can simply increment each iteration.
 
Thank you that sounds like a plan , could you maybe give me a small example of how to set a cell value ? Thank you very much by the way it's been driving me insane
 
Back
Top