locked
Get All Data From A Column in a DataGridView RRS feed

  • Question

  • I have a data grid view that contains several columns.  The first column is the only column I care about and it contains Result IDs.  The view may be filter from time to time.  I want to get the Result IDs from all the visible rows and put them into a table variable so I can use ADO.Net to send that table to a SQL Server stored procedure.  I currently use a loop (see below).  Is there a way to do this quicker without a loop?  I'm new to VB.Net and I was hoping .Net would have an impressive method to do the same thing the loop below is doing.  Anyone have any ideas?

      Private Function prvfnc_GetResultIDs(ByVal dgv As DataGridView, ByRef dt As DataTable) As String
    
        Dim rw As DataRow
    
        Try
          ' add ResultID column to data table
          dt.Columns.Add(New DataColumn("ResultID", GetType(Integer)))
    
          ' add ResultIDs to column of data table
          For Each dgvr As DataGridViewRow In dgv.Rows
    
            rw = dt.NewRow
    
            rw("ResultID") = dgvr.Cells("ResultID").Value
    
            dt.Rows.Add(rw)
    
          Next dgvr
    
          Return "Success"
    
        Catch ex As Exception
    
          Call pubsub_ErrorHandler(Me.GetType.Name, System.Reflection.MethodBase.GetCurrentMethod().Name, ex.Message)
          Return "Failed"
    
        End Try
    
      End Function
    

    Thanks, Ryan


    Ryan
    Friday, July 22, 2011 3:46 PM

Answers

  • you could use LINQ to get all the values from that 1 column, but you'd have to loop through them to add them to your datatable anyway
    thanks for any help
    • Marked as answer by Liliane Teng Sunday, July 31, 2011 1:36 PM
    Friday, July 22, 2011 5:41 PM
  • Hello Ryan,

    Thanks for your post.

    As .paul._ suggested, you could use LINQ to implement. Please check below code which queries sno column value from Table_1 using LINQ. Please have a try.

      Dim d As DataTable = DbDataSet.Tables("Table_1")
    
      Dim query = _
    
      From f In d.AsEnumerable()
    
      Select New With _
    
        { _
    
     .SnameDataGridViewTextBoxColumn = f.Field(Of Integer)("sno") _
    
         }
    
      End
    
    

     More information about LINQ to DataSet from MSDN:

    http://msdn.microsoft.com/en-us/library/bb386921.aspx (Creating a DataTable From a Query (LINQ to DataSet)

    By the way, for more information about LINQ technology, please try Data Platforum Development forums for more better suggestions because more this aspect experts live there. Thanks for your understanding.

    http://social.msdn.microsoft.com/Forums/en-US/category/dataplatformdev (Data Platform Development Forums)

    If you have any concerns, please feel free to follow up.

    Best regards


    Liliane Teng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Liliane Teng Sunday, July 31, 2011 1:36 PM
    Monday, July 25, 2011 5:35 AM

All replies

  • you could use LINQ to get all the values from that 1 column, but you'd have to loop through them to add them to your datatable anyway
    thanks for any help
    • Marked as answer by Liliane Teng Sunday, July 31, 2011 1:36 PM
    Friday, July 22, 2011 5:41 PM
  • Hello Ryan,

    Thanks for your post.

    As .paul._ suggested, you could use LINQ to implement. Please check below code which queries sno column value from Table_1 using LINQ. Please have a try.

      Dim d As DataTable = DbDataSet.Tables("Table_1")
    
      Dim query = _
    
      From f In d.AsEnumerable()
    
      Select New With _
    
        { _
    
     .SnameDataGridViewTextBoxColumn = f.Field(Of Integer)("sno") _
    
         }
    
      End
    
    

     More information about LINQ to DataSet from MSDN:

    http://msdn.microsoft.com/en-us/library/bb386921.aspx (Creating a DataTable From a Query (LINQ to DataSet)

    By the way, for more information about LINQ technology, please try Data Platforum Development forums for more better suggestions because more this aspect experts live there. Thanks for your understanding.

    http://social.msdn.microsoft.com/Forums/en-US/category/dataplatformdev (Data Platform Development Forums)

    If you have any concerns, please feel free to follow up.

    Best regards


    Liliane Teng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Liliane Teng Sunday, July 31, 2011 1:36 PM
    Monday, July 25, 2011 5:35 AM
  • this is probably not the best method

       Dim rowcount As Integer
        rowcount = ds.Tables(INDEX).Rows.Count
        Dim currentrow As Integer
       currentrow = 0
    
        Do While currentrow < rowcount
          Dim results As String
          results = (ds.Tables(INDEX).Rows(currentrow)(NAME OF Coulmn.ToString)
        Loop
    


    rowcount is defined as the amount of rows in the table . so if there is 11 rows ,it is 11

    ds = Dataset

    INDEX = table index or could be name

    Current row - Starts at 0 (because the first row is 0 )

    The program loops everytime adding 1 to currentrow until it reaches the row count .

    each time getting the data of of the current row from the column .

     

    Hope this helps

    Monday, July 25, 2011 7:38 AM
  • this is probably not the best method

     

      Dim rowcount As Integer
      rowcount = ds.Tables(INDEX).Rows.Count
      Dim currentrow As Integer
      currentrow = 0
    
      Do While currentrow < rowcount
       Dim results As String
       results = (ds.Tables(INDEX).Rows(currentrow)(NAME OF Coulmn.ToString)
      Loop
    

     


    rowcount is defined as the amount of rows in the table . so if there is 11 rows ,it is 11

    ds = Dataset

    INDEX = table index or could be name

    Current row - Starts at 0 (because the first row is 0 )

    The program loops everytime adding 1 to currentrow until it reaches the row count .

    each time getting the data of of the current row from the column .

     

    Hope this helps

    He is asking for looping Grid column, not dataset column
    Malayalam SMS Website http://smsmallu.brinkster.net
    Monday, July 25, 2011 9:45 AM
  • Hi Ryan,

    What about this problem on your side now? Would you mind letting us know the result of the suggestions? If you have any concerns, please feel free to follow up.

    Have a nice day.

    Best regards


    Liliane Teng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, July 27, 2011 9:00 AM