How to get special cell from DataTable Row

Mansour_Dalir 2,036 Reputation points
2023-07-24T17:09:49.7033333+00:00

hi

    Dim MyDataTable As New DataTable 
        MyDataTable.Columns.Add("Group")
        MyDataTable.Columns.Add("Type")
        MyDataTable.Columns.Add("Size")
        MyDataTable.Columns.Add("Location")
        MyDataTable.Rows.Add({"CABLE", "MV", "1x240", "Air Building"})
        dim intStart=1 
       dim intCount=2
      	Dim Result= MyDataTable.Rows(0).ItemArray.AsEnumerable().Select(Of String)(Function(h) h).ToArray
        'Out to : MV,1x240,Air Building
    
    ```

Developer technologies | VB
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    2023-07-25T01:45:08.68+00:00

    Hi @Mansour_Dalir ,

    Since you do not explain the meaning of intStart and intCount, the following code speculates that intStart is the index of the starting column, and intCount is the number of columns that need to be fetched backwards.

    Dim Result As String() = MyDataTable.Rows(0).ItemArray.Skip(intStart - 1).Take(intCount + 1).Select(Function(item) item.ToString()).ToArray()
    

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Dewayne Basnett 1,381 Reputation points
    2023-07-24T18:40:16.3466667+00:00

    A guess

            Dim MyDataTable As New DataTable
            MyDataTable.Columns.Add("Group")
            MyDataTable.Columns.Add("Type")
            MyDataTable.Columns.Add("Size")
            MyDataTable.Columns.Add("Location")
            MyDataTable.Rows.Add({"CABLE", "MV", "1x240", "Air Building"})
            Dim intStart As Integer = 1
            Dim intCount As Integer = 2
            Dim Result() As String = MyDataTable.Rows(0).ItemArray.Skip(1).Cast(Of String)().ToArray
            'Result(0) : MV
            'Result(1) : 1x240
            'Result(2) : Air Building
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.