it-partners Home   |  Site Map
 Support
       Bits and Pieces

Loading selected rows in itGrid into a .Net DataTable

This code can be used to populate a .Net DataGrid with rows that have been selected in itGrid. To use this code, ensure that you have used:

itGrid.SelectionMode = itGrid6.SelectionModeConstants.itSelectListBox

Here's some code that calls the DataTable populating procedure...

If itGrid1.SelectedCount > 0 Then

  Dim dt As System.Data.DataTable
  dt = DataTableFromSelectedRows(itGrid1)

End If

And here's the code that populates the DataTable...

Private Function DataTableFromSelectedRows(ByVal grid As AxitGrid6.AxitGrid) As System.Data.DataTable

  Dim dt As New System.Data.DataTable("test")
  Dim colType As System.Type
  Dim col As Integer
  Dim col1 As Integer
  Dim col2 As Integer
  Dim row As Integer
  Dim var As Object
  Dim newRow As System.Data.DataRow
  Dim start As Integer
  Dim finish As Integer

  With grid

    col1 = .FixedCols
    col2 = (.Cols - 1)
    ReDim var(col2 - col1, 0)

    ' Load the DataTable columns
    For col = col1 To col2
      Select Case .Columns(col).DataType

        Case itGrid6.DataTypeConstants.itString
          colType = GetType(String)

        Case itGrid6.DataTypeConstants.itLong
          colType = GetType(Integer)

        Case itGrid6.DataTypeConstants.itInteger
          colType = GetType(Short)

        Case itGrid6.DataTypeConstants.itBoolean
          colType = GetType(Boolean)

        ' You can add more data types here, or use this as a general purpose
        ' catch-all
        Case Else
          colType = GetType(System.Object)

      End Select
      dt.Columns.Add(col.ToString, colType)
    Next col

    start = Environment.TickCount

    ' Load the DataTable rows
    For row = .FixedRows To (.Rows - 1)
      If .get_IsSelected(row) Then

        var = .get_ValueMatrix(row, col1, row, col2)
        newRow = dt.NewRow
        For col = 0 To (col2 - col1)
          newRow.Item(col) = DirectCast(var, System.Object(,))(col, 0)
        Next col
        dt.Rows.Add(newRow)

      End If
    Next row

    finish = Environment.TickCount

  End With

  DataTableFromSelectedRows = dt

  ' WOW!, that was quick :)
  MsgBox("That took " & CType((finish - start) / 1000, Double ).ToString("0.00 seconds"))

End Function

^ top