Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy data in a DatagridView to a Listbox

Status
Not open for further replies.

b98mr1

Programmer
Feb 23, 2005
20
GB
Hi all,

I have just made a move into Visual Studio 2005 and am having a few problems when trying to copy data in a datagridview and displaying each row in a listbox. It's only the data in the first column that I need copied and the datatype is 'double'.

Here is the code that I have written to do this:

Dim row As Integer
For row = 0 To <datagrid>.Rows.Count - 1
Dim cellvalue As Object = Me.<datagrid>.(0, row)
Me.<listbox>.Items.Add(cellvalue.ToString)
Next row

This however does not display the data correctly. Instead of displaying the value in the listbox, the following appears:

DatagridViewTextBoxCell{ColumnIndex=0, RowIndex=0}

Can anyone point me in the right direction as to why this is not displaying correctly. I have been going around in circles for two days now!

Ross.
 
You need to initialize an object as a DataGridViewRow and use that to cycle through the items.


dim rowdata as DataGridViewRow
.
.
.
Listbox1.items.add(rowdata.cells(0).value.tostring)
...


If you're using a databound grid without allow add, then you could use the For each command...

Dim rowdata as Datagridviewrow

For each rowdata in DatagridView1.rows
Listbox1.items.add(rowdata.cells(0).value.tostring)
Next

If you're using allow adds, there'll be a null value row at the end you'll need to handle.

HTH
 
Thanks very much. That worked a treat....
 
Try this:


Dim i As Integer

With Me.DataGridView1
For i = 0 To .RowCount - 2

Me.ListBox1.Items.Add(.Item(0, i).Value)
Next
End With


Notice the '-2' ...
If there is any problem post reply.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top