?
Solved

Converting from DataGridView to DataSet

Posted on 2011-04-22
3
Medium Priority
?
431 Views
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 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

0
Comment
Question by:tobin46
  • 2
3 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 35450214
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.
0
 
LVL 1

Author Comment

by:tobin46
ID: 35450247
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.
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 35450318
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.

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

850 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