If you select rows from a grid on one form and are trying to copy those selected rows to a grid on another form how would you accomplish this?
I'm trying the code below but the selected rows never copy over. I've tried using
loTable = oTable.Copy but this copies every row from the current grid instead of just the selected rows. Any help would be appreciated.
Private Sub cmdMerge_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdMerge.Click
Dim oTable As DataTable
Dim loTable As DataTable
Dim oItem As Infragistics.Win.UltraWinGrid.UltraGridRow
Try
Cursor.Current = Cursors.WaitCursor
If uGridMain.Selected.Rows.Count < 2 Then
MessageBox.Show(
"You must select at least two(2) " & AddressAlias & " records to be merged.", "Merge Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Exit Sub
End If
Dim oForm As New frmMergeRecords
oTable = uGridMain.DataSource
loTable = oTable.Clone
oForm.uGridMain.DataSource = loTable
For Each oItem In uGridMain.Selected.Rows
oForm.uGridMain.BeginUpdate()
For intI As Integer = 1 To oItem.Cells.Count - 1
If TypeOf oItem.Cells(intI).Value Is Boolean Then
oForm.uGridMain.Rows.TemplateAddRow.Cells(intI).Value = oItem.Cells(intI).Value
Else
oForm.uGridMain.Rows.TemplateAddRow.Cells(intI).Value = oItem.Cells(intI).Text
Next
oForm.uGridMain.EndUpdate()
oForm.Text =
"Merge " & AddressAlias
oForm.AddressMerge =
True
oForm.ShowDialog()
LoadListView()
Catch ex As Exception
HandleError(ex, Name,
"cmdMerge_Click" )
Finally
Cursor.Current = Cursors.Default
End Try
End Sub
Hi,
One thing I notice is that you are never committing the TemplateAddRow or moving to a new one. You are just writing and re-writing into the same TemplateAddRow.
But in any case, as I mentioned before, using the TemplateAddRow and adding rows through the UI of the grid in code is sort've an odd choice. The UI is for users, but you have better options in code.
What I would do is add your rows to the new cloned table that you bound the grid to. That will be much easier and more efficient than using the TemplateAddRow.The code would look something like this:
For Each oItem In uGridMain.Selected.Rows Dim oDataRow As DataRow = CType(oItem.ListObject, DataRowView).Row Dim oNewDataRow As DataRow = oTable.NewRow() For Each oColumn As DataColumn In oTable.Columns oNewDataRow(oColumn) = oDataRow(oColumn) Next oTable.Rows.Add(oNewDataRow) Next oItem oTable.AcceptChanges()
Icopied it into notepad and then to here. Hope this helps.
Dim oTable As DataTable Dim loTable As DataTable Dim oItem As Infragistics.Win.UltraWinGrid.UltraGridRow Dim oForm As New frmMergeRecords Try Cursor.Current = Cursors.WaitCursor If uGridMain.Selected.Rows.Count < 2 Then MessageBox.Show("You must select at least two(2) " & AddressAlias & " records to be merged.", "Merge Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) Exit Sub End If
oTable = uGridMain.DataSource loTable = oTable.Clone oForm.uGridMain.DataSource = loTable
oForm.uGridMain.BeginUpdate() For Each oItem In uGridMain.Selected.Rows For intI As Integer = 0 To oItem.Cells.Count - 1 If TypeOf oItem.Cells(intI).Value Is Boolean Then oForm.uGridMain.Rows.TemplateAddRow.Cells(intI).Value = oItem.Cells(intI).Value Else oForm.uGridMain.Rows.TemplateAddRow.Cells(intI).Value = oItem.Cells(intI).Text End If Next Next oItem oForm.uGridMain.EndUpdate()
oForm.Text = "Merge " & AddressAlias oForm.AddressMerge = True oForm.ShowDialog() LoadListView() Catch ex As Exception HandleError(ex, Name, "cmdMerge_Click") Finally Cursor.Current = Cursors.Default End Try End Sub
Yes, I am trying to only show the selected rows from the first grid in the second grid. And unless I'm missing something I'm not changing the datasource on the main grid but I am setting the datasource on the other forms grid.
This code is really hard to read - our forums apparently don't like copying and pasting from Visual Studio on some machines. You might want to post a small sample project or maybe paste your code into Notepad and then copy it from there so it strips out the formatting.
I don't see anything obviously wrong with the code you have here. It looks like you are looping through the selected rows in the first grid and copying the values into the second grid.
You are using the TemplateAddRow, which is a bit odd, though. You are essentially copying the rows via the user interface, which is a strange thing to do in code. There are certainly more efficient and easier ways to do it.
Are you trying to have the second grid show only the selected rows in the first grid? Or do you want to add the selected rows to the existing rows in the second grid?
Why are you changing the DataSource on the main grid?
Yes thats not working either.