943,102 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 2616
  • VB.NET RSS
Feb 12th, 2010
0

Get data bound to a datagird to a datatable (VS2003)

Expand Post »
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.

VB.NET Syntax (Toggle Plain Text)
  1. Dim DV As DataView
  2. Dim tempDT As DataTable
  3.  
  4. tempDT = objdsInvoice.Tables("INVOICE_ITEM").Copy
  5. tempDT.DefaultView.RowFilter = "INVOICE_NUMBER = " & Val(editINVOICE_NUMBER.Text)
  6. 'objdsInvoice.Tables("INVOICE_ITEM").DefaultView.RowFilter = "INVOICE_NUMBER = " & Val(editINVOICE_NUMBER.Text)
  7.  
  8. dtBeforeEdit = tempDT.DefaultView
  9.  
  10. Once I got all the data in dtBeforeEdit dataview I am retrieving the data using loop -
  11.  
  12. For Each dr As DataRow In dtAfterEdit.Rows
  13.  
  14. .....
  15. ....
  16. Perform action on the rows.
  17.  
  18. 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.
Attached Thumbnails
Click image for larger version

Name:	Master-detail.PNG
Views:	179
Size:	42.9 KB
ID:	13624  
Last edited by adatapost; Feb 16th, 2010 at 11:35 am. Reason: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
ukshir is offline Offline
18 posts
since Feb 2010
Feb 13th, 2010
0
Re: Get data bound to a datagird to a datatable (VS2003)
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?
Reputation Points: 14
Solved Threads: 12
Junior Poster
kplcjl is offline Offline
140 posts
since Sep 2009
Feb 16th, 2010
0
Re: Get data bound to a datagird to a datatable (VS2003)
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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
ukshir is offline Offline
18 posts
since Feb 2010
Feb 16th, 2010
0
Re: Get data bound to a datagird to a datatable (VS2003)
>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.
VB.NET Syntax (Toggle Plain Text)
  1. objdsInvoice.Relations("put_your_relation_name").ChildTable
Moderator
Reputation Points: 2134
Solved Threads: 1227
Posting Genius
adatapost is offline Offline
6,524 posts
since Oct 2008
Feb 20th, 2010
0
Re: Get data bound to a datagird to a datatable (VS2003)
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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
ukshir is offline Offline
18 posts
since Feb 2010
Feb 20th, 2010
0
Re: Get data bound to a datagird to a datatable (VS2003)
Parent-child (datatable) relation is needed and also you have to add bindingSource for two different tables.

Take a look at sample,

Form1.vb
VB.NET Syntax (Toggle Plain Text)
  1. Imports System.Data
  2. Public Class Form1
  3. 'Dataset
  4. Dim ds As New DataSet("MyDb")
  5. Dim bindStu As BindingSource
  6. Dim bindMks As BindingSource
  7.  
  8. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  9. Dim dt1 As New DataTable("Student")
  10.  
  11. dt1.Columns.Add("Roll", GetType(Integer))
  12. dt1.Columns.Add("Name")
  13.  
  14. 'Primary key
  15. dt1.PrimaryKey = New DataColumn() {dt1.Columns(0)}
  16.  
  17. dt1.Rows.Add(1, "A")
  18. dt1.Rows.Add(2, "B")
  19.  
  20. 'Second table - Marks
  21. Dim dt2 As New DataTable("Marks")
  22. dt2.Columns.Add("Roll", GetType(Integer))
  23. dt2.Columns.Add("Subject")
  24. dt2.Columns.Add("MarksObt", GetType(Integer))
  25.  
  26. dt2.Rows.Add(1, "Sub1", 50)
  27. dt2.Rows.Add(1, "Sub2", 80)
  28. dt2.Rows.Add(2, "Sub1", 87)
  29. dt2.Rows.Add(2, "Sub2", 77)
  30. dt2.Rows.Add(2, "Sub3", 98)
  31.  
  32.  
  33. ds.Tables.Add(dt1)
  34. ds.Tables.Add(dt2)
  35.  
  36. 'Create parent-child relationship
  37. Dim rel1 As New DataRelation("rel1", dt1.Columns(0), dt2.Columns(0))
  38. ds.Relations.Add(rel1)
  39.  
  40. 'Create binding source for parent table
  41. bindStu = New BindingSource(ds, "Student")
  42.  
  43. 'Create binding source for child table
  44. bindMks = New BindingSource(bindStu, "rel1")
  45.  
  46.  
  47. TextBox1.DataBindings.Add("Text", bindStu, "Roll")
  48. TextBox2.DataBindings.Add("Text", bindStu, "Name")
  49.  
  50. DataGridView1.DataSource = bindMks
  51. End Sub
  52.  
  53. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  54. bindStu.Position += 1
  55. End Sub
  56. End Class

Form1.Designer.vb
VB.NET Syntax (Toggle Plain Text)
  1. <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
  2. Partial Class Form1
  3. Inherits System.Windows.Forms.Form
  4.  
  5. 'Form overrides dispose to clean up the component list.
  6. <System.Diagnostics.DebuggerNonUserCode()> _
  7. Protected Overrides Sub Dispose(ByVal disposing As Boolean)
  8. Try
  9. If disposing AndAlso components IsNot Nothing Then
  10. components.Dispose()
  11. End If
  12. Finally
  13. MyBase.Dispose(disposing)
  14. End Try
  15. End Sub
  16.  
  17. 'Required by the Windows Form Designer
  18. Private components As System.ComponentModel.IContainer
  19.  
  20. 'NOTE: The following procedure is required by the Windows Form Designer
  21. 'It can be modified using the Windows Form Designer.
  22. 'Do not modify it using the code editor.
  23. <System.Diagnostics.DebuggerStepThrough()> _
  24. Private Sub InitializeComponent()
  25. Me.TextBox1 = New System.Windows.Forms.TextBox
  26. Me.TextBox2 = New System.Windows.Forms.TextBox
  27. Me.DataGridView1 = New System.Windows.Forms.DataGridView
  28. Me.Button1 = New System.Windows.Forms.Button
  29. CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).BeginInit()
  30. Me.SuspendLayout()
  31. '
  32. 'TextBox1
  33. '
  34. Me.TextBox1.Location = New System.Drawing.Point(164, 38)
  35. Me.TextBox1.Name = "TextBox1"
  36. Me.TextBox1.Size = New System.Drawing.Size(100, 20)
  37. Me.TextBox1.TabIndex = 0
  38. '
  39. 'TextBox2
  40. '
  41. Me.TextBox2.Location = New System.Drawing.Point(164, 64)
  42. Me.TextBox2.Name = "TextBox2"
  43. Me.TextBox2.Size = New System.Drawing.Size(100, 20)
  44. Me.TextBox2.TabIndex = 0
  45. '
  46. 'DataGridView1
  47. '
  48. Me.DataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
  49. Me.DataGridView1.Location = New System.Drawing.Point(89, 144)
  50. Me.DataGridView1.Name = "DataGridView1"
  51. Me.DataGridView1.Size = New System.Drawing.Size(240, 150)
  52. Me.DataGridView1.TabIndex = 1
  53. '
  54. 'Button1
  55. '
  56. Me.Button1.Location = New System.Drawing.Point(164, 103)
  57. Me.Button1.Name = "Button1"
  58. Me.Button1.Size = New System.Drawing.Size(75, 23)
  59. Me.Button1.TabIndex = 2
  60. Me.Button1.Text = "Button1"
  61. Me.Button1.UseVisualStyleBackColor = True
  62. '
  63. 'Form1
  64. '
  65. Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
  66. Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
  67. Me.ClientSize = New System.Drawing.Size(461, 320)
  68. Me.Controls.Add(Me.Button1)
  69. Me.Controls.Add(Me.DataGridView1)
  70. Me.Controls.Add(Me.TextBox2)
  71. Me.Controls.Add(Me.TextBox1)
  72. Me.Name = "Form1"
  73. Me.Text = "Form1"
  74. CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).EndInit()
  75. Me.ResumeLayout(False)
  76. Me.PerformLayout()
  77.  
  78. End Sub
  79. Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
  80. Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
  81. Friend WithEvents DataGridView1 As System.Windows.Forms.DataGridView
  82. Friend WithEvents Button1 As System.Windows.Forms.Button
  83.  
  84. End Class
Moderator
Reputation Points: 2134
Solved Threads: 1227
Posting Genius
adatapost is offline Offline
6,524 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Please Help with Printing TabControl tabs
Next Thread in VB.NET Forum Timeline: How Best to Parse a Text Log File





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC