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 !!!
 
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.
 
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:
Did you try this?

dataset1.tables(0).rows.add (dataset1.tables(1).rows(gatagrid2.currentrowindex))
dgPOrderItems.datasource=dataset1.tables(0)
 
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:
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.
 
i'm completely confused with the DataGrid ... sigh !!!

VB.NET:
 objDS.Tables("Item_No").Rows.Add(objDS2.Tables("Item_No").Rows(dgPOproductinfo.CurrentRowIndex))
		dgPOrderItems.DataSource = objDS2.Tables(0)

still not work ...
 
Did you read all of my last post? You can't just copy the row as the data is different!
To get the cell data you do need 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.
 
i'm sorry!! i do not know the structure of datagrid coding much ...
i'm studying it .. i think i can work it out soon !!
thanks!!
i will ask you when i have any problem!!

Merry Christmas !!!! :)
 
i still couldn't work it out .........................
HELP ............ going to die ... !!!!
How COME !!:confused: :(


may be could you simply tell me how to pass one row from one datagrid to another ????
 
An unhandled exception of type 'System.Exception' occurred in system.windows.forms.dll

Additional information: Complex DataBinding accepts as a data source either an IList or an IListSource

while i try this ...
VB.NET:
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		dgPOrderItems.DataSource = objDS2.Tables(0).Rows(dgPOproductinfo.CurrentRowIndex)
 
	End Sub
and it underline me with this :
VB.NET:
datagrid1.item(datagrid1.selectedrowindex,0)

an error
An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication3.exe
Additional information: Object reference not set to an instance of an object
when i try :
VB.NET:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim ds As DataSet
		ds.Tables(0).Rows.Add(ds.Tables(1).Rows(dgPOproductinfo.CurrentRowIndex))
		dgPOrderItems.DataSource = ds.Tables(0)
	End Sub
 
Last edited:
When a dataGrid has had it's dataSource property set to a dataSet (or dataTable), you need only alter the dataSet (or dataTable) in order to alter the data shown by the dataGrid.

One problem I see with the above code is that you're filling the dataSet (objDS) then setting the dataSource of the first dataGrid to that dataSet; you then clear the dataSet which removes all the data; now the first dataGrid won't display any data.

You can still use one dataSet, but you'll need two dataTables within that dataSet if you want the two dataGrids to continue showing the different data.
It looks like your function 'Search_tblUser' returns a dataset; have it return a dataTable instead. Also good to know is that you can use the 'Fill' function of a dataAdapter to fill a dataTable as well as a dataSet (when you set the Fill functions' parameter to a dataSet, it's actually creating a new dataTable in the dataSet and filling that).

Once you have that down, you can then pass data between the two dataTables in a variety of ways. If the dataTables have the same fields, you can simply pass the dataRow. If not, you'll need to create a new dataRow, set it's field values, then add it to the dataGrid's dataSource.
 
Back
Top