locked
how to convert datagridview.rows(x) into an a string array? RRS feed

  • Question

  • I know i can do this to add a new row:
    Code Block

    dim stringArr() as string = {"value","value","value"}
    datagridview1.rows.add(stringArr)



    now I want to do the opposite and take the active row and convert it into an string array
    this doesn't seem to work:
    Code Block

    dim x as integer
    for x = 0 to To DataGridView1.Rows.Count - 1
    dim stringArr() as string = datagridview1.rows(x)          'it give me a type error saying can't convert to 1 dimensional array of string.. etc...
    Next



    so how can I take the active row and convert it into a string array?

    -thanks
    Tuesday, November 27, 2007 8:25 PM

Answers

  • This will work.  I will leave it up to you to modify the code as needed depending on if you are permitting multiple selection or not.

     

    Code Block

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim stringArr() As String = {"value1", "value2", "value3"}

    Me.DataGridView1.Rows.Add(stringArr)

    Me.DataGridView1.SelectAll()

    Dim arr As New ArrayList

    For i As Integer = 0 To Me.DataGridView1.ColumnCount - 1

    Dim c As DataGridViewSelectedRowCollection = Me.DataGridView1.SelectedRows

    For x As Integer = 1 To c.Count - 1

    If Not c.Item(x) Is Nothing Then

    arr.Add(c.Item(x).Cells(i).Value)

    End If

    Next

    Next

    Dim stringArr2() As String = arr.ToArray(GetType(String))

    End Sub

     

     

     

    Tuesday, November 27, 2007 10:18 PM