Results 1 to 3 of 3

Thread: Taking your datagridview and putting it into a dataset

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Location
    In my head
    Posts
    913

    Taking your datagridview and putting it into a dataset

    This is a fairly short and sweet function that will take your datagridview and dump it into a dataset. This was created for/using VS 2005.

    Code:
    Public Function DatagridviewToDataset(ByVal dgv As DataGridView) As System.Data.DataSet
            Dim ds As New System.Data.DataSet
    
            'Take the data and structure from the datagridview and return it as a dataset.  You can use 
            '"Imports System.Data" declaration at the top of your project/class and remove the system.data 
            'from the various parts of this function.
    
            Try
                'Add a new table to the dataset
                ds.Tables.Add("Main")
    
                'Add the columns
                Dim col As System.Data.DataColumn
    
                'For each colum in the datagridveiw add a new column to your table
                For Each dgvCol As DataGridViewColumn In dgv.Columns
                    col = New System.Data.DataColumn(dgvCol.Name)
                    ds.Tables("Main").Columns.Add(col)
                Next
    
                'Add the rows from the datagridview
                Dim row As System.Data.DataRow
                Dim colcount As Integer = dgv.Columns.Count - 1
    
                For i As Integer = 0 To dgv.Rows.Count - 1
                    row = ds.Tables("Main").Rows.Add
    
                    For Each column As DataGridViewColumn In dgv.Columns
                        row.Item(column.Index) = dgv.Rows.Item(i).Cells(column.Index).Value
                    Next
    
                Next
    
                Return ds
            Catch ex As Exception
                'Catch any potential errors and display them to the user
                MessageBox.Show("Error Converting from DataGridView" & ex.InnerException.ToString, _
                "Error Converting from DataGridView", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Return Nothing
            End Try
        End Function
    Edit: I flubbed the rowsetup and had to change it
    Last edited by dminder; Apr 30th, 2007 at 02:36 PM.
    Platforms of choice: Visual Studio 2005/2008 Professional : Visual Studio 2010 Enterprise : PHP - Notepad++/WAMP

    Please Rate If I helped you.
    Please remember to mark threads as closed if your issue has been resolved.

    Reserved Words in Access | Connection Strings

  2. #2
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    Re: Taking your datagridview and putting it into a dataset

    !!Sorry wrong window!!
    Yes!!!
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work


  3. #3
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    Re: Taking your datagridview and putting it into a dataset

    sorry again
    Yes!!!
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width