I have a form developed using the Dataform wizard (VS 2003). The Form shows Master-Detail data. The details are showen in a Datagrid. The form automatically loads the data in the Form_load event. Now I want to store the data from the datagrid to a datatable.
I am using following code to get the details showen in the datagrid into a dataview.
The objdsInvoice is the dataset that holds the Invoice And Invoice_Item data.

Dim DV As DataView
        Dim tempDT As DataTable

        tempDT = objdsInvoice.Tables("INVOICE_ITEM").Copy
        tempDT.DefaultView.RowFilter = "INVOICE_NUMBER = " & Val(editINVOICE_NUMBER.Text)
        'objdsInvoice.Tables("INVOICE_ITEM").DefaultView.RowFilter = "INVOICE_NUMBER = " & Val(editINVOICE_NUMBER.Text)

        dtBeforeEdit = tempDT.DefaultView

Once I got all the data in dtBeforeEdit dataview I am retrieving the data using loop - 

        For Each dr As DataRow In dtAfterEdit.Rows

		.....
		....
		Perform action on the rows.

        Next

Is there any better way to get the datagird values that are currently displayed into a datatable.
The datagrid is bound to the details table in the Master-detail type dataset.

Edited 6 Years Ago by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

Attachments Master-detail.PNG 42.9 KB

I don't understand. You have a datagrid that is displaying data. To do that you need to bind a dataset to it. In order to have data in a dataset, it must have one or more datatables in it, to store your data.
Why don't you trace the code generated for you to find the datatables that are already there?

Hi kplcjl,
I have all the data loaded in the dataset "objdsInvoice" for both the tables INVOICE and INVOICE_ITEM using the autogenerated code.
INVOICE And INVOICE_ITEM tables are related using the INVOICE_NUMBER key. When I get the data from the objdsInvoice.Tables("INVOICE_ITEM") i get all the data in the INVOICE_ITEM table and not the only data that is currently displayed in the datagrid (i.e. INVOICE_ITEM rows for a particular INVOICE that is shown in the datagrid). I need a method to get the INVOICE_ITEM rows that are shown in the Datagrid.
Currently what I am doing is filtering the records from the INVOICE_ITEM table in the Dataset and storing it in a dataview.
tempDT = objdsInvoice.Tables("INVOICE_ITEM").Copy
tempDT.DefaultView.RowFilter = "INVOICE_NUMBER = " & Val(editINVOICE_NUMBER.Text)
'objdsInvoice.Tables("INVOICE_ITEM").DefaultView.RowFilter = "INVOICE_NUMBER = " & Val(editINVOICE_NUMBER.Text)

dtBeforeEdit = tempDT.DefaultView

I would like to know if there is any method by which I can directly get all the rows currently shown in the datagrid in a datatable.

I hope I am making it clear. Please let me know if you need any other information.

>I would like to know if there is any method by which I can directly get all the rows currently shown in the datagrid in a datatable.

Use ChildTable propety of relation object.

objdsInvoice.Relations("put_your_relation_name").ChildTable

Thanks adatapost!
I tried the childtable to get the datatable from the dataset, but it give all the data from the child table. I need the filtered data which is currently being shown in the datagrid.
Say I have 4 INVOICE and the corresponding INVOICE_ITEMS loaded into the objInvoice dataset. Not while navigating, I am on Invoice No. 3. The datagrid is showing the INVOICE_ITEMS for Invoice No.3. Now I want the INVOICE_ITEMS for Invoice No.3 to a Datatable. I know that I can filter out the INVOICE_ITEMS from the dataset and get them in a datatableview. But I want to know if there is any method by which I can get the data using any property of the datagrid.

e.g. if I want to get the current row count displayed in the above datagrid i am using -
grdINVOICE_ITEM.BindingContext(grdINVOICE_ITEM.DataSource, grdINVOICE_ITEM.DataMember).Count

I want to know if there is something similar I can use to get the current data shown in the datagrid.

Parent-child (datatable) relation is needed and also you have to add bindingSource for two different tables.

Take a look at sample,

Form1.vb

Imports System.Data
Public Class Form1
    'Dataset
    Dim ds As New DataSet("MyDb")
    Dim bindStu As BindingSource
    Dim bindMks As BindingSource

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim dt1 As New DataTable("Student")

        dt1.Columns.Add("Roll", GetType(Integer))
        dt1.Columns.Add("Name")

        'Primary key
        dt1.PrimaryKey = New DataColumn() {dt1.Columns(0)}

        dt1.Rows.Add(1, "A")
        dt1.Rows.Add(2, "B")

        'Second table - Marks
        Dim dt2 As New DataTable("Marks")
        dt2.Columns.Add("Roll", GetType(Integer))
        dt2.Columns.Add("Subject")
        dt2.Columns.Add("MarksObt", GetType(Integer))

        dt2.Rows.Add(1, "Sub1", 50)
        dt2.Rows.Add(1, "Sub2", 80)
        dt2.Rows.Add(2, "Sub1", 87)
        dt2.Rows.Add(2, "Sub2", 77)
        dt2.Rows.Add(2, "Sub3", 98)


        ds.Tables.Add(dt1)
        ds.Tables.Add(dt2)

        'Create parent-child relationship
        Dim rel1 As New DataRelation("rel1", dt1.Columns(0), dt2.Columns(0))
        ds.Relations.Add(rel1)

        'Create binding source for parent table
        bindStu = New BindingSource(ds, "Student")

        'Create binding source for child table
        bindMks = New BindingSource(bindStu, "rel1")


        TextBox1.DataBindings.Add("Text", bindStu, "Roll")
        TextBox2.DataBindings.Add("Text", bindStu, "Name")

        DataGridView1.DataSource = bindMks
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        bindStu.Position += 1
    End Sub
End Class

Form1.Designer.vb

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.TextBox2 = New System.Windows.Forms.TextBox
        Me.DataGridView1 = New System.Windows.Forms.DataGridView
        Me.Button1 = New System.Windows.Forms.Button
        CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(164, 38)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(100, 20)
        Me.TextBox1.TabIndex = 0
        '
        'TextBox2
        '
        Me.TextBox2.Location = New System.Drawing.Point(164, 64)
        Me.TextBox2.Name = "TextBox2"
        Me.TextBox2.Size = New System.Drawing.Size(100, 20)
        Me.TextBox2.TabIndex = 0
        '
        'DataGridView1
        '
        Me.DataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
        Me.DataGridView1.Location = New System.Drawing.Point(89, 144)
        Me.DataGridView1.Name = "DataGridView1"
        Me.DataGridView1.Size = New System.Drawing.Size(240, 150)
        Me.DataGridView1.TabIndex = 1
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(164, 103)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(75, 23)
        Me.Button1.TabIndex = 2
        Me.Button1.Text = "Button1"
        Me.Button1.UseVisualStyleBackColor = True
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(461, 320)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.DataGridView1)
        Me.Controls.Add(Me.TextBox2)
        Me.Controls.Add(Me.TextBox1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
    Friend WithEvents DataGridView1 As System.Windows.Forms.DataGridView
    Friend WithEvents Button1 As System.Windows.Forms.Button

End Class
This article has been dead for over six months. Start a new discussion instead.