如何在VB.net中将datagridview中的特定行添加到列表框中

时间:2014-12-02 09:45:18

标签: vb.net datagridview listbox

按下按钮时,我希望数据网格视图中特定行的值放在列表框中。例如,当按下Budgie按钮时,我想要将关于Budgie的所有细节(位于第1行)放在列表​​框中(这样我就可以计算成本)但是,当我按下按钮时,所有显示在列表框中的按钮是'DataGridViewRow {Index = 1}'我需要更改哪些内容而不是显示值?

  

Private Sub btnBudgie_Click(sender as Object,e As EventArgs)处理btnBudgie.Click

     

Me.lstSales.Items.Add(DataGridView1.Rows(1))

     

End Sub

1 个答案:

答案 0 :(得分:0)

如果选择整行,则此方法有效:

Dim Item As String = ""
    Try
        Dim c As Integer = DataGridView1.SelectedRows(0).Cells.Count - 1

        If c <= 0 Then
            c = 0
        End If

        For x = 0 To c
            Dim cell As DataGridViewCell = DataGridView1.CurrentRow.Cells(x)

            If x = DataGridView1.SelectedRows(0).Cells.Count - 1 Then
                Item = Item & cell.Value
            Else
                Item = Item & cell.Value & " - "
            End If
        Next

        ListBox1.Items.Add(Item)
    Catch ex As Exception
        MessageBox.Show("An error occured. Have you selected a whole row?" & Environment.NewLine & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try