Link to home
Start Free TrialLog in
Avatar of tobin46
tobin46

asked on

Converting from DataGridView to DataSet

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 below....how 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

        Try

            'Add a new table to the dataset
            ds.Tables.Add("Table")

            '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)
                ds.Tables("Table").Columns.Add(col)
            Next

            '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
                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

Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

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.
Avatar of tobin46
tobin46

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial