Question DataGridView <- For each object in one column

mac-duff

Member
Joined
Dec 21, 2009
Messages
24
Programming Experience
Beginner
Hello,

I would like to select all items of all rows but just from one column. In a dropbox its quite easy but how do I do this in field?

This is how I do right now...

VB.NET:
            Dim i As Integer
            Dim rowindex As DataGridViewRow
            Dim columnindex As DataGridViewColumn

            'For Each columnindex In HardwareDataGridView.Columns
            For Each rowindex In HardwareDataGridView.Rows
                i = i + 1
                MsgBox(HardwareDataGridView(rowindex).Value.ToString())
                'MsgBox(HardwareDataGridView.Item(HardwareDataGridView.CurrentRow.Index, HardwareDataGridView.CurrentCell.Value).Value)
            Next

The column I would like to read is DataGridViewTextBoxColumn32.Index

Thanks

Btw. VB2008 ;)

ok, so its works:

VB.NET:
        Dim i As Integer
            Dim rowindex As DataGridViewRow
            Dim columnindex As DataGridViewColumn

            'For Each columnindex In HardwareDataGridView.Columns
            For Each rowindex In HardwareDataGridView.Rows

                MsgBox(HardwareDataGridView.Rows(i).Cells(DataGridViewTextBoxColumn32.Index).Value)
                i = i + 1
                'MsgBox(HardwareDataGridView.Item(HardwareDataGridView.CurrentRow.Index, HardwareDataGridView.CurrentCell.Value).Value)
            Next

But I dont like it, Can I not do it without the i?
 
Last edited:
What's the point of a For Each loop if you're not going to use the loop counter? The whole point of a For Each loop is that you get each item in the list so that you can use it in some way. In your case you're looping through the rows in the grid so, in the loop, you're supposed to use the current row. It also isn't logical to use 'rowindex' as the loop counter because each item in the Rows collection is a row, not a row index.
VB.NET:
For Each row As DataGridViewRow In HardwareDataGridView.Rows
    MessageBox.Show(CStr(row.Cells(columnIndex).Value))
Next
'columnIndex' is, of course, the index of the column you want to get the values from.
 
Last edited:
Back
Top