Converting from DataGridView to DataSet

Posted on 2011-04-22
Last Modified: 2012-05-11
Hi - In my app, user enters a parent record - slip_id, job_id, etc.
There are 4 datagridviews that then contain data "linked" back to the this parent record.  

The construct the DGV's manually via code.  

When the user goes to save the entire slip, I'm trying to convert each of them to a dataset but I have an issue:
- When the user enters the DGV and begins to enter data, there is a second row created can I stop this from happening?  I only want to see one row at a time.

There are columns in the DGV that are hidden, and I populate them behind the scenes.  So when this second row is created, I populate the cells with data from the "parent" record, but the other values are null.  Based on my code, it forces me to add a row with a bunch of nulls to the dataset.

Here is the code I'm using to convert from datagrid....
Public Function DG_TO_Dataset(ByVal dgv As DataGridView) As DataSet

        Dim ds As New DataSet


            'Add a new table to the dataset

            'Add the columns
            Dim col As DataColumn

            'For each colum in the datagridveiw add a new column to your table
            For Each dgvCol As DataGridViewColumn In dgv.Columns
                col = New DataColumn(dgvCol.Name)

            'Add the rows from the datagridview
            Dim row As DataRow
            Dim colcount As Integer = dgv.Columns.Count - 1

            For i As Integer = 0 To dgv.Rows.Count - 1

                row = ds.Tables("Table").Rows.Add

                For Each column As DataGridViewColumn In dgv.Columns
                    row.Item(column.Index) = dgv.Rows(i).Cells(column.Index).Value


            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

Question by:tobin46
    LVL 83

    Expert Comment

    This is the built in functionality of the grid. Try setting AllowUserToAddRows property to false but then you would have to provide a way of adding new rows yourself.
    LVL 1

    Author Comment

    I think this may be inefficient....the whole process....

    How would you suggest as a best practice to get the rows from the datagridview into a datset so that I can update a database with it?  This is new data entry.
    LVL 83

    Accepted Solution

    I think the best way is to use a datatable as a datasource of the grid. You would not have to worry about transferring from grid to dataset.


    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now