How can i pass Data from one DataGrid to another ?

rex028

Well-known member
Joined
Nov 18, 2004
Messages
50
Programming Experience
Beginner
i'm using Two Windows Form ... for example Form1 and Form2

i have DataGrid in each Form

then i wanna pass the Data to Form1's Datagrid from Form2's Datagrid ....

how can i do it ????

can you give me some hints ??

please help !!!
 

TPM

Well-known member
Joined
Dec 7, 2004
Messages
623
Location
CA
Programming Experience
3-5
make a global dataset by putting this code at the top of your form below public class form1:
public shared dataset as new dataset

Then read the data into it, then in form 1 :
datagrid.source=dataset

In form 2:
datagrid2.source=form1.dataset
 

rex028

Well-known member
Joined
Nov 18, 2004
Messages
50
Programming Experience
Beginner
For example:

in datagrid 2 : there are Four rows ...
i select one row .. then i want to add this row into datagrid 1

is it the same as you teach me ??
 

TPM

Well-known member
Joined
Dec 7, 2004
Messages
623
Location
CA
Programming Experience
3-5
Hmm I see, well you could do it like that but have 2 datasets. 1 shared public and 1 private. Dagagrid2 uses private, Datagrid1 uses public. when user clicks the button you just add the data from the private dataset to the public.
 

rex028

Well-known member
Joined
Nov 18, 2004
Messages
50
Programming Experience
Beginner
sorry could you show me how to coding look like ?

becoz i'm really worse in VB.NET !!

PLEASE HELP ME !!
 

TPM

Well-known member
Joined
Dec 7, 2004
Messages
623
Location
CA
Programming Experience
3-5
form 1 :
public shared DS1 as dataset
datagrid1.datasource = DS1


Form 2 :
onload...
Dim DS2 as dataset
...read data into dataset...
Gatagrid2.datasource = DS2

onclick..
dataset1.rows.add (dataset2.rows(gatagrid2.currentrowindex))
 

rex028

Well-known member
Joined
Nov 18, 2004
Messages
50
Programming Experience
Beginner
VB.NET:
  dataset1.rows.add (dataset2.rows(gatagrid2.currentrowindex))

it says "rows" is not a member of "System.Data.Dataset"
 

TPM

Well-known member
Joined
Dec 7, 2004
Messages
623
Location
CA
Programming Experience
3-5
Sorry missed tables in there :
dataset1.tables(0).rows.add (dataset2.rows(gatagrid2.currentrowindex))
 

rex028

Well-known member
Joined
Nov 18, 2004
Messages
50
Programming Experience
Beginner
i still not really understand how it works ...

may be i describe my situation more specific ...

i have a Purchase Order ... with One DataGrid contain PO
then
i press a search Items button a ItemSearch windows from pop up ...
i do item search there ... record display in a datagrid there ..
when i double click it .. the selected item record will pass to Purchase Order's datagrid ..
 

rex028

Well-known member
Joined
Nov 18, 2004
Messages
50
Programming Experience
Beginner
sorry ...
my problem is i still couldn't pass the Data from ItemSearch to PurchaseOrder

actually .. there are a problem in PurchaseOrder too ...

that's when i create a new PurchaseOrder ... i have to hold the new PO in Datagrid until all purchase items are add into the PO ... right !!

so how can i hold them in the datagrid ...

and

i follow the solution you teach me ...

i get an error that is DS1 can not be declare even i set DS1 is public shared !!
 

TPM

Well-known member
Joined
Dec 7, 2004
Messages
623
Location
CA
Programming Experience
3-5
So it's your dataset your having problems with then, not the data...

Public shared? is the dataset on another form? If so you'll need to call the form then the dataset eg form1.ds1

Did you remember the new when diming your dataset? ie dim ds1 as new dataset
 

rex028

Well-known member
Joined
Nov 18, 2004
Messages
50
Programming Experience
Beginner
as you taught me b4 ..
i can't show different data in two datagrids in same windows form now !!
i have two DataGrid : dgItemsDetails , dgPO

now ..
dgItemsDetails on left hand side and dgPO on right hand side...
there are two buttons between them..
what i want to do is ... i select a row of itemdetails and press "add" ...
it will add to the dgPO ...

i'm using one Dataset only ...
my code like this ..
VB.NET:
  Private Sub GetPoDetails()
		objDS.Tables.Clear()
		objDS = objUser.Search_tblUser(" SELECT  p.* , s.Supplier_ID, s.Supplier_Name FROM tbl_SUPPLIER s, tbl_PURCHASE p where (p.PO_ID = '" & txtPOID.Text & "') and (p.Supplier_ID = s.Supplier_ID ) ")
		Try
			If objDS.Tables(0).Rows.Count <= 0 Then
				MsgBox("Record Not Found")
			End If
			If objDS.Tables(0).Rows.Count > 0 Then
				ts1.MappingName = objDS.Tables(0).ToString
				With tc1
					.HeaderText = "Item No."
					.MappingName = "Item_No"
					.Width = 60
				End With
				With tc2
					.HeaderText = "Item Price"
					.MappingName = "Item_Price"
					.Width = 60
				End With
				With tc3
					.HeaderText = "Item Quantity"
					.MappingName = "Item_Quantity"
					.Width = 80
				End With
				ts1.GridColumnStyles.Add(tc1)
				ts1.GridColumnStyles.Add(tc2)
				ts1.GridColumnStyles.Add(tc3)
				dgPOrderItems.TableStyles.Add(ts1)
				dgPOrderItems.DataSource = objDS.Tables(0).DefaultView

				With objDS.Tables(0)
					txtPOBranchID.Text = objFormat.FormatString(.Rows(0).Item("Branch_ID"))
					txtPOID.Text = objFormat.FormatString(.Rows(0).Item("PO_ID"))
					txtPOItemNo.Text = objFormat.FormatString(.Rows(0).Item("Item_No"))
					txtPOItemQua.Text = objFormat.FormatString(.Rows(0).Item("Item_Quantity"))
					txtPOItemPrice.Text = objFormat.FormatString(.Rows(0).Item("Item_Price"))
					txtPODay.Text = objFormat.FormatString(.Rows(0).Item("Order_Date").Day.ToString)
					txtPOMonth.Text = objFormat.FormatString(.Rows(0).Item("Order_Date").Month.ToString)
					txtPOYear.Text = objFormat.FormatString(.Rows(0).Item("Order_Date").Year.ToString)
					txtPOModifitedDate.Text = objFormat.FormatString(.Rows(0).Item("Modified_Date"))
					txtPOModifitedBy.Text = objFormat.FormatString(.Rows(0).Item("Modified_By"))
					txtPOSupplierID.Text = objFormat.FormatString(.Rows(0).Item("s.Supplier_ID"))
					txtPOsuppliername.Text = objFormat.FormatString(.Rows(0).Item("Supplier_Name"))

				End With
			End If
		Catch ex As Exception
		End Try
	End Sub

VB.NET:
 Private Sub GetItemDetails()
		objDS.Tables.Clear()
		objDS = objUser.Search_tblUser("Select * from tbl_ITEM where Item_Type = '" & cboPOItemType.Text & "'")
		Try
			dgPOproductinfo.DataSource = objDS.Tables(0)
		Catch ex As Exception
		End Try
	End Sub
 

TPM

Well-known member
Joined
Dec 7, 2004
Messages
623
Location
CA
Programming Experience
3-5
Ohh man...

Ok :

1. You said you where having a problem with DS1, yet you didn't post the code for that..

2. Leave things like table styles out untill it actually works, it'll make your code easyer to quickly read through.

3. You still have not given the exact error message or location.

4 to help you get the error location add messagebox.show(ex.message) after you catch ex as exception.
 

rex028

Well-known member
Joined
Nov 18, 2004
Messages
50
Programming Experience
Beginner
Oops !!! i'm so sorry !! i had type something wrong again !!
sorry sorry!!!

i mean i can display two different data in two datagrids now !!
and
i want to make two datagrids able to transfer data between them ...

This is my DataAdapter Class:
VB.NET:
 	Public Function AddDataSet(ByVal strSQL As String, ByVal arrData() As String, ByVal intTotalNumberOfFields As Integer) As Boolean
		Try 'Initialize DataAdapter with SQL and connection string
			SetConnectString()
			mobjDA = New OleDb.OleDbDataAdapter(strSQL, strConnect)
			mobjCB = New OleDb.OleDbCommandBuilder(mobjDA)
			objDS.Clear()
			mobjDA.Fill(objDS)
			'Add new record to local memory
			objRow = objDS.Tables(0).NewRow
			For i = 0 To intTotalNumberOfFields
				objRow(i) = arrData(i)
			Next i
			objDS.Tables(0).Rows.Add(objRow)
 
			mobjDA.Update(objDS)
			AddDataSet = True
		Catch ex As Exception
			str = "Error in AddDataSet. Error No. " & Err.Number & " : " & Err.Description
			GetErrorMessage(str)
			AddDataSet = False
			Exit Function
		End Try
	End Function
 
Public Function SearchDataSet(ByVal strSQL As String) As DataSet
		SetConnectString()
		'Initialize DataAdapter with SQL and connection string
		mobjDA = New OleDb.OleDbDataAdapter(strSQL, strConnect)
		mobjCB = New OleDb.OleDbCommandBuilder(mobjDA)
		objDS.Clear()
		'Fill in DataSet
		Try
			mobjDA.Fill(objDS)
			SearchDataSet = objDS 'Return dataset
		Catch ex As Exception
			str = "Error in SearchDataSet. Error No. " & Err.Number & " : " & Err.Description
			GetErrorMessage(str)
			Exit Function
		End Try
	End Function

This is my DataGrids coding:
VB.NET:
 Private Sub GetPoDetails()
		objDS.Tables.Clear()
		objDS = objUser.Search_tblUser(" SELECT p.* , s.Supplier_ID, s.Supplier_Name FROM tbl_SUPPLIER s, tbl_PURCHASE p where (p.PO_ID = '" & txtPOID.Text & "') and (p.Supplier_ID = s.Supplier_ID ) ")
		Try
			If objDS.Tables(0).Rows.Count <= 0 Then
				MsgBox("Record Not Found")
			End If
			If objDS.Tables(0).Rows.Count > 0 Then
				ts1.MappingName = objDS.Tables(0).ToString
				With tc1
					.HeaderText = "Item No."
					.MappingName = "Item_No"
					.Width = 60
				End With
				With tc2
					.HeaderText = "Item Price"
					.MappingName = "Item_Price"
					.Width = 60
				End With
				With tc3
					.HeaderText = "Item Quantity"
					.MappingName = "Item_Quantity"
					.Width = 80
				End With
				ts1.GridColumnStyles.Add(tc1)
				ts1.GridColumnStyles.Add(tc2)
				ts1.GridColumnStyles.Add(tc3)
				dgPOrderItems.TableStyles.Add(ts1)
				dgPOrderItems.DataSource = objDS.Tables(0).DefaultView
 
				With objDS.Tables(0)
					txtPOBranchID.Text = objFormat.FormatString(.Rows(0).Item("Branch_ID"))
					txtPOID.Text = objFormat.FormatString(.Rows(0).Item("PO_ID"))
					txtPOItemNo.Text = objFormat.FormatString(.Rows(0).Item("Item_No"))
					txtPOItemQua.Text = objFormat.FormatString(.Rows(0).Item("Item_Quantity"))
					txtPOItemPrice.Text = objFormat.FormatString(.Rows(0).Item("Item_Price"))
					txtPODay.Text = objFormat.FormatString(.Rows(0).Item("Order_Date").Day.ToString)
					txtPOMonth.Text = objFormat.FormatString(.Rows(0).Item("Order_Date").Month.ToString)
					txtPOYear.Text = objFormat.FormatString(.Rows(0).Item("Order_Date").Year.ToString)
					txtPOModifitedDate.Text = objFormat.FormatString(.Rows(0).Item("Modified_Date"))
					txtPOModifitedBy.Text = objFormat.FormatString(.Rows(0).Item("Modified_By"))
					txtPOSupplierID.Text = objFormat.FormatString(.Rows(0).Item("s.Supplier_ID"))
					txtPOsuppliername.Text = objFormat.FormatString(.Rows(0).Item("Supplier_Name"))
 
				End With
			End If
		Catch ex As Exception
		End Try
	End Sub
	Private Sub GetItemDetails()
		objDS.Tables.Clear()
		objDS = objUser.Search_tblUser("Select * from tbl_ITEM where Item_Type = '" & cboPOItemType.Text & "'")
		Try
			dgPOproductinfo.DataSource = objDS.Tables(0)
		Catch ex As Exception
		End Try
	End Sub


i have two DataGrids : dgPOproductinfo, dgPOrderItems

when i create new PO .. dgPOrderItems display a blank datagrid with columns name...
then
search the item and display on dgPOproductinfo ,
if
i select a row of item and press "add" .. this item detail will pass to dgPOrderItems .. a new row ...
then
save as a new Purchase Order !!
 
Last edited:

TPM

Well-known member
Joined
Dec 7, 2004
Messages
623
Location
CA
Programming Experience
3-5
Did you try this?

dataset1.tables(0).rows.add (dataset1.tables(1).rows(gatagrid2.currentrowindex))
dgPOrderItems.datasource=dataset1.tables(0)
 

rex028

Well-known member
Joined
Nov 18, 2004
Messages
50
Programming Experience
Beginner
an error
An unhandled exception of type " System.IndexOutOfRangeException" occurred in system.data.dll
Additional informtation: " Dataset1 not found"
VB.NET:
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		objDS.Tables(0).Rows.Add(objDS.Tables(1).Rows(dgPOproductinfo.CurrentRowIndex))
		dgPOrderItems.DataSource = objDS.Tables(0)
	End Sub

do i have to set the colums?

actually
dgPOproductinfo contains: Item_No, Item_Type, Item_Name, Item_Desc, Item_Amount, Item_Discount_Rate, Item_Bonus .... etc ..

dgPOrderItems contains: Branch_ID, PO_ID, Supplier_ID, Order_Date, Item_No, Item_Price, Item_Quantity ....

when i press this button ... i want the Item_No pass to dgPOrderItems ...

so am i have to set the Colum something ?
or
what else should i do ?
 
Last edited:

TPM

Well-known member
Joined
Dec 7, 2004
Messages
623
Location
CA
Programming Experience
3-5
You would need to change dataset1 to the name of your dataset....

You never said the data was going to be compleatly different! You can't just copy it, as then the item number will be the branchID etc. You'll need to select just what you need and add create a row to add. to get the info out you need you'll use:
datagrid1.item(datagrid1.selectedrowindex,0)

Where datagrid1 if the name of the datagrid you want to get the info from, and 0 if the column number.
 
Top Bottom