SelectedItems should stay Selected - sql

I have a Problem with my two DataGrids
The first DataGrid is used to Show Data from an SQL Database.
Now i want that, if i select one row and save it to the Datagrid2, it stays selected and won't Change.
The Problem is now that if i Change the Row, the DataGrid2 changes too.
I hope you understand my Problem.
Here's the Code
Public Sub SelectItem()
Try
Dim rows As List(Of Integer) = New List(Of Integer)
For Each cell As DataGridCellInfo In DataGrid1.SelectedCells
rows.Add(DataGrid1.Items.IndexOf(cell.Item))
DataGrid1.SelectedItems.Clear()
Next
For Each Item As Integer In rows
If (Item < DataGrid1.Items.Count) Then
DataGrid1.SelectedItems.Add(DataGrid1.Items.GetItemAt(Item))
DataGrid2.ItemsSource = DataGrid1.SelectedItems
End If
Next
Catch ex As Exception
GeneralMergeTools.ShowError(ex, GeneralMergeTools.FatalError.CriticalError, "ContentControl1.SelectedItem")
End Try
End Sub
Kind Regards
EDIT:
I'll add here my Code for Future People who have the same or a similar Problem.
Public Sub SelectItem() 'Auf Knopfdruck
Try
Dim dt As DataTable = CType(Me.DataGrid1.ItemsSource, DataView).Table.Clone
For Each r1 As System.Data.DataRowView In Me.DataGrid1.SelectedItems
Dim r2 As DataRow = dt.NewRow
For Each c As System.Data.DataColumn In dt.Columns
r2.Item(c.ColumnName) = r1.Row(c.ColumnName)
Next
dt.Rows.Add(r2)
DataGrid2.ItemsSource = dt.DefaultView
Next
Catch ex As Exception
GeneralMergeTools.ShowError(ex, GeneralMergeTools.FatalError.CriticalError, "ContentControl1.SelectedItem")
End Try
End Sub

You share the same instance of Items between 2 grids:
DataGrid2.ItemsSource = DataGrid1.SelectedItems
So basically when you change property IsSelected in first grid by selecting it, on the second grid it's exactly the same object having also this change, so second grid also change SelectedItem accordingly.
To fix this behavior you need to have different instances of objects in your ItemSource.

So this is my very well working Code for this matter:
Public Sub SelectItem()
Try
Dim dt As DataTable = CType(Me.DataGrid1.ItemsSource, DataView).Table.Clone
For Each r1 As System.Data.DataRowView In Me.DataGrid1.SelectedItems
Dim r2 As DataRow = dt.NewRow
For Each c As System.Data.DataColumn In dt.Columns
r2.Item(c.ColumnName) = r1.Row(c.ColumnName)
Next
dt.Rows.Add(r2)
DataGrid2.ItemsSource = dt.DefaultView
Next

Related

Pasting new records to a Bound DataGridView

Apologies if this has already been asked. If so, I am unable to find a simple solution. I am trying to allow a user to copy/paste multiple records in a DataGridView (the in memory copy of the data, to be saved later when the user clicks the save button) and cannot find anything that works. It probably is because there is something I do not understand about all of this.
I set up a standard edit form with Visual Studio's drag/table into a form, so it's using a BindingSource control and all the other controls that come with doing that. It works just fine when manually entering something in the new row one by one, so it seems to be set up correctly, but when it comes to adding a record (or multiples) using code, nothing seems to work.
I tried a few things as outline in the code below. Could someone please at least steer me in the right direction? It cannot be that difficult to paste multiple records.
I run this when the user presses Control-V (the clipboard correctly holds the delimited strings):
Private Sub PasteClipboard()
If Clipboard.ContainsText Then
Dim sLines() As String = Clipboard.GetText.Split(vbCrLf)
For Each sLine As String In sLines
Dim Items() As String = sLine.Split(vbTab)
Dim drv As DataRowView = AdjustmentsBindingSource.AddNew()
drv.Item(1) = Items(0)
drv.Item(2) = Items(1)
drv.Item(3) = Items(2)
drv.Item(4) = Items(3)
'Error on next line : Cannot add external objects to this list.
AdjustmentsBindingSource.Add(drv)
Next
End If
End Sub
EDIT
(the bindingsource is bound to a dataadapter, which is bound to a table in an mdb file, if that helps understand)
I adjusted the inner part of the code to this:
If (RowHasData(Items)) Then
Dim drv As DataRowView = AdjustmentsBindingSource.AddNew()
drv.Item("FontName") = Items(0)
drv.Item("FontSize") = Items(1)
drv.Item("LetterCombo") = Items(2)
drv.Item("Adjustment") = Items(3)
drv.Item("HorV") = Items(4)
End If
It kinda works, but it also adds a blank row before the 2 new rows. Not sure where that is coming from, as I have even included your RowHasData() routine...
I would think that “attemp3” SHOULD work, however, it is unclear “what” the AdjustmentsBindingSource’s DataSource is. Is it a List<T> or DataTable?
If I set the BinngSource.DataSource to a DataTable, then attempt 3 appears to work. Below is an example that worked.
Private Sub PasteClipboard2()
If Clipboard.ContainsText Then
Dim sLines() As String = Clipboard.GetText.Split(vbCrLf)
For Each sLine As String In sLines
Dim Items() As String = sLine.Split(vbTab)
If (RowHasData(Items)) Then
Dim drv As DataRowView = AdjustmentsBindingSource.AddNew()
drv.Item("FontName") = Items(0)
drv.Item("FontSize") = Items(1)
drv.Item("LetterCombo") = Items(2)
drv.Item("Adjustment") = Items(3)
drv.Item("HorV") = Items(4)
End If
Next
End If
End Sub
This appears to work in my tests. I added a small function (RowHasData) to avoid malformed strings causing problems. It simply checks the size (at least 5 items) and also checks to make sure a row actually has “some” data. If a row is just empty strings, then it is ignored.
Private Function RowHasData(items As String())
If (items.Count >= 5) Then
For Each item In items
If (item <> "") Then Return True
Next
End If
Return False
End Function
I am guessing it would be just as easy to add the new rows “directly” to the BindingSource’s DataSource. In the example below, the code is adding the row “directly” to the DataTable that is used as a DataSource to the BindingSource. I am confident you could do the same thing with a List<T> by simply adding a new object to the list. Below is a complete example using a BindingSource and a DataTable. This simply adds the rows to the bottom of the table.
Dim gridTable1 As DataTable
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
PasteClipboard()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
gridTable1 = GetTable()
FillTable(gridTable1)
AdjustmentsBindingSource.DataSource = gridTable1
AdjustmentsDataGridView.DataSource = AdjustmentsBindingSource
End Sub
Private Function GetTable() As DataTable
Dim dt = New DataTable()
dt.Columns.Add("FontName", GetType(String))
dt.Columns.Add("FontSize", GetType(String))
dt.Columns.Add("LetterCombo", GetType(String))
dt.Columns.Add("Adjustment", GetType(String))
dt.Columns.Add("HorV", GetType(String))
Return dt
End Function
Private Sub FillTable(dt As DataTable)
For index = 1 To 10
dt.Rows.Add("Name_" + index.ToString(), "Size_" + index.ToString(), "Combo_" + index.ToString(), "Adjust_" + index.ToString(), "HorV_" + index.ToString())
Next
End Sub
Private Sub PasteClipboard()
If Clipboard.ContainsText Then
Dim sLines() As String = Clipboard.GetText.Split(vbCrLf)
Try
Dim dataRow As DataRow
For Each sLine As String In sLines
Dim Items() As String = sLine.Split(vbTab)
If (RowHasData(Items)) Then
dataRow = gridTable1.NewRow()
dataRow("FontName") = Items(0)
dataRow("FontSize") = Items(1)
dataRow("LetterCombo") = Items(2)
dataRow("Adjustment") = Items(3)
dataRow("HorV") = Items(4)
gridTable1.Rows.Add(dataRow)
End If
Next
Catch ex As Exception
MessageBox.Show("Error: " + ex.Message)
End Try
End If
End Sub
Private Function RowHasData(items As String())
If (items.Count >= 5) Then
For Each item In items
If (item <> "") Then Return True
Next
End If
Return False
End Function
Hope the code helps…
Last but important, I am only guessing that you may have not “TESTED” the different ways users can “SELECT” data and “how” other applications “copy” that selected data. My previous tests using the WIN-OS “Clipboard” can sometimes give unexpected results. Example, if the user selects multiple items using the ”Ctrl” key to “ADD” to the selection, extra rows appeared in the Clipboard if the selection was not contiguous. My important point is that using the OS clipboard is quirky IMHO. I recommend LOTS of testing on the “different” ways the user can select the data. If this is not an issue then the code above should work.

set new DataGridViewComboBoxCell for after validation of previous DataGridViewTextBoxCell in same column and row

I have this DataGridView and it has a DataGridViewTextBoxColumn where user can type a number, and after he types it I perform a search to find previous records under that number.
I want to show this records so user can select one of them, or keep the value typed, which means he wants to create a new record.
For that, I want to replace each DataGridViewTextBoxCell for a DataGridViewComboBoxCell with those options when user has finished typing.
However, it is raising this exception when I try to permform this replacement: "System.InvalidOperationException" in System.Windows.Forms.dll. Additional information: The operation is not valid because it results in a reentering call to the function SetCurrentCellAddressCore.
Here is my code (I already tried to handle CellLeave instead of CellValidated):
Private Sub DataGridViewDebitos_CellValidated(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridViewDebitos.CellValidated
DataGridViewDebitos.EndEdit(DataGridViewDataErrorContexts.Commit)
If e.ColumnIndex = ColumnDebito.Index Then
Dim cellDebito = DataGridViewDebitos.Rows(e.RowIndex).Cells(ColumnDebito.Index)
Dim numDebito = String.Concat(If(cellDebito.Value, "").ToString.Where(Function(c) Char.IsLetterOrDigit(c)))
If TypeOf cellDebito Is DataGridViewTextBoxCell AndAlso numDebito.Length >= 3 Then
Dim prcsa As New List(Of JObject) 'In real version, prcsa is populated by an external function, but it doesn't affect the result
Dim j = New JObject
j.SetProperty("id", 0)
j.SetProperty("nome", cellDebito.Value)
prcsa.Insert(0, j) 'This option is always present, it allows user to keep simply what was typed
'Exception hapens here
DataGridViewDebitos(cellDebito.ColumnIndex, cellDebito.RowIndex) =
New DataGridViewComboBoxCell With {
.DataSource = prcsa,
.DisplayMember = "nome",
.FlatStyle = FlatStyle.Flat,
.ValueMember = "id",
.Value = prcsa(0).Value(Of Integer)("id")}
End If
End If
End Sub
Thank you very much
I've put the problematic statement into a Lambda sub via .BeginInvoke and this solved the problem, only I don't know why...
Can anyone explain the mechanics of it to me? Thank you!
Private Sub DataGridViewDebitos_CellValidated(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridViewDebitos.CellValidated
DataGridViewDebitos.EndEdit(DataGridViewDataErrorContexts.Commit)
If e.ColumnIndex = ColumnDebito.Index Then
Dim cellDebito = DataGridViewDebitos.Rows(e.RowIndex).Cells(ColumnDebito.Index)
Dim numDebito = String.Concat(If(cellDebito.Value, "").ToString.Where(Function(c) Char.IsLetterOrDigit(c)))
If TypeOf cellDebito Is DataGridViewTextBoxCell AndAlso numDebito.Length >= 3 Then
Dim prcsa As New List(Of JObject) 'In real version, prcsa is populated by an external function, but it doesn't affect the result
Dim j = New JObject
j.SetProperty("id", 0)
j.SetProperty("nome", cellDebito.Value)
prcsa.Insert(0, j) 'This option is always present, it allows user to keep simply what was typed
'Exception hapens here
DataGridViewDebitos.BeginInvoke(
Sub()
DataGridViewDebitos(cellDebito.ColumnIndex, cellDebito.RowIndex) =
New DataGridViewComboBoxCell With {
.DataSource = prcsa,
.DisplayMember = "nome",
.FlatStyle = FlatStyle.Flat,
.ValueMember = "id",
.Value = prcsa(0)("id")}
End Sub)
End If
End If
End Sub

Adding Data to DataTable

This code is recording the inserted IDs in arraylist and when press on button it should create loop for this array and get the data of each ID from the array and put it in Datatable and add the new data while looping to the datatable and finaly show it in datagridview .
The problem in the result when I insert one record it works fine but when I insert more than one the datagridview shows just the last one , what the mistake that I Done ?!!
In Mainform
Public Inserted_record_hold_dt As New DataTable
Public Inserted_record_dt As New DataTable
Public Sub Addcolumnstodatagrid()
Inserted_record_dt.Columns.Add("ID")
Inserted_record_dt.Columns(0).AutoIncrement = True
Inserted_record_dt.Columns.Add("drawingname")
Inserted_record_dt.Columns.Add("serial")
End Sub
and call this in main_Load
Addcolumnstodatagrid()
And this in the show button when click to loop on the array list that already have the latest ID's that has been added
Private Sub show_btn_Click(sender As System.Object, e As System.EventArgs) Handles show_btn.Click
Dim InsertedID As Integer
Inserted_record_dt.Clear()
Dim R As DataRow = Inserted_record_dt.NewRow
'Loop For each ID in the array "Inserted_List_Array"
For Each InsertedID In mainadd.Inserted_List_Array
'MsgBox(InsertedID.ToString)
Dim cmd As New SqlCommand("select drawingname , serial from main where drawingid = '" & InsertedID & "'", DBConnection)
DBConnection.Open()
Inserted_record_hold_dt.Load(cmd.ExecuteReader)
Try
R("drawingname") = Inserted_record_hold_dt.Rows(0).Item(0)
R("serial") = Inserted_record_hold_dt.Rows(0).Item(1)
Inserted_record_dt.Rows.Add(R)
Catch
End Try
'MsgBox("added")
DBConnection.Close()
cmd = Nothing
Inserted_record_hold_dt.Clear()
Next
sendmail.Show()
sendmail.Mail_DGView.DataSource = Inserted_record_dt
End Sub
Please tell me what is the problem in my code .
Your mistake is in declaring the R variable just one time outside the loop. In this way you continuously replace the values on the same instance of a DataRow and insert always the same instance.
Just move the declaration inside the loop
For Each InsertedID In mainadd.Inserted_List_Array
......
Try
Dim R As DataRow = Inserted_record_dt.NewRow
R("drawingname") = Inserted_record_hold_dt.Rows(0).Item(0)
R("serial") = Inserted_record_hold_dt.Rows(0).Item(1)
Inserted_record_dt.Rows.Add(R)
Catch
....
Next
Another important thing to do is to remove the empty Try/Catch because you are just killing the exception (no message, no log) and thus you will never know if there are errors in this import. At the end you will ship a product that could give incorrect results to your end user.

Merge two identical DataTables results in DataRowState.Modified

Am I wrong assuming that if two identical DataTables are merged the state of each row will be preserved?
Take a look at this simple example. It creates two identical tables and merge the updated table with original table. But the returned table in original.GetChanges() is not Nothing as expected. Also, the state of each row in the original table are changed to Modified.
So what am I missing? Do I really have to create my own merge method to achieve this?
Public Sub Test()
Dim original As DataTable = Me.CreateTableWithData()
Dim updated As DataTable = Me.CreateTableWithData()
Dim preserveChanges As Boolean = True
Dim msAction As MissingSchemaAction = MissingSchemaAction.Ignore
original.Merge(updated, preserveChanges, msAction)
Dim changes As DataTable = original.GetChanges()
MessageBox.Show(String.Format("Count={0}", If((changes Is Nothing), 0, changes.Rows.Count)), Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
If (Not changes Is Nothing) Then changes.Dispose() : changes = Nothing
updated.Dispose() : updated = Nothing
original.Dispose() : original = Nothing
End Sub
Private Function CreateTableWithData() As DataTable
Dim table As New DataTable("TEST")
table.Columns.Add("ID", GetType(Integer))
table.Columns.Add("VALUE", GetType(String))
table.PrimaryKey = New DataColumn() {table.Columns(0)}
table.Rows.Add(1, "Value 1")
table.Rows.Add(2, "Value 2")
table.AcceptChanges()
Return table
End Function
Output: Count=2
Edit - The workaround
The following code is a workaround for this strange(?) behavior.
Private Shared Sub Merge(target As DataTable, source As DataTable, preserveChanges As Boolean, msa As MissingSchemaAction)
target.Merge(source, preserveChanges, msa)
Dim row As DataRow
Dim column As DataColumn
Dim acceptChanges As Boolean
For Each row In target.Rows
If ((row.RowState = DataRowState.Modified) AndAlso ((row.HasVersion(DataRowVersion.Original)) AndAlso (row.HasVersion(DataRowVersion.Default)))) Then
acceptChanges = True
For Each column In target.Columns
If (Not Object.Equals(row.Item(column, DataRowVersion.Original), row.Item(column, DataRowVersion.Default))) Then
acceptChanges = False
Exit For
End If
Next
If (acceptChanges) Then
row.AcceptChanges()
End If
End If
Next
acceptChanges = Nothing
column = Nothing
row = Nothing
End Sub
After some time of working with DataTable merge I found the best solution to merging data, preserving changes and not setting the RowState to Modified for all of the existing rows.
What I discovered is that all of the rows in the original DataTable would have their RowState set to Modified if you use the DataTable Merge and pass True as the preserve changes property. If you pass false instead, the RowStates remain the same.
Going back to the documentation for the DataTable.Merge(DataTable, Boolean, MissingSchemaAction) Method I found this:
...In this scenario, the GetChanges method is first invoked. That method returns a second DataTable optimized for validating and merging. This second DataTable object contains only the DataTable and DataRow objects objects that were changed, resulting in a subset of the original DataTable...
From there I started to realize that the this merge is not really intended to be used with the original data directly... instead you should merge against the table returned by the GetChanges method (passing true in preserving changes) and then merge the changes table into the original source passing false for the preserving changes parameter.
To demonstrate this I have created the following class:
Class TableManger
Implements ComponentModel.INotifyPropertyChanged
Private _table1 As System.Data.DataTable
Private _table2 As System.Data.DataTable
Private _changesDetected As Integer = 0
Public ReadOnly Property Table1
Get
Return _table1
End Get
End Property
Public ReadOnly Property ChangesDetected As Integer
Get
Return _changesDetected
End Get
End Property
Public Sub New()
_table1 = CreateTableWithData()
_table1.AcceptChanges()
AddHandler _table1.RowChanged, New System.Data.DataRowChangeEventHandler(AddressOf Row_Changed)
End Sub
Public Sub MergeTables()
_table2 = _table1.Clone
Dim tableRows As New List(Of System.Data.DataRow)
For Each r In _table1.Rows
Dim dr2 = _table2.NewRow
For Each col As System.Data.DataColumn In _table1.Columns
dr2(col.ColumnName) = r(col.ColumnName)
Next
_table2.Rows.Add(dr2)
tableRows.Add(dr2)
Next
_table2.AcceptChanges()
If _table2.Rows.Count > 0 Then
_table2.Rows(0)(1) = "TB2 Changed"
End If
If _table1.Rows.Count > 0 Then
'_table1.Rows(0)(1) = "TB1 Change"'
_table1.Rows(1)(1) = "TB1 Change"
End If
_changesDetected = 0
Dim perserveChanges As Boolean = True
Dim msAction As System.Data.MissingSchemaAction = System.Data.MissingSchemaAction.Ignore
Dim changes As System.Data.DataTable = _table1.GetChanges()
If changes IsNot Nothing Then
changes.Merge(_table2, perserveChanges, msAction)
_table1.Merge(changes, False, msAction)
Else
_table1.Merge(_table2, False, msAction)
End If
MessageBox.Show(String.Format("Changes in Change Table: {0} {1}Changes Detected: {2}", If((changes Is Nothing), 0, changes.Rows.Count), System.Environment.NewLine, _changesDetected), "Testing")
RaiseEvent PropertyChanged(Me, New ComponentModel.PropertyChangedEventArgs("Table1"))
RaiseEvent PropertyChanged(Me, New ComponentModel.PropertyChangedEventArgs("ChangesDetected"))
End Sub
Private Sub Row_Changed(ByVal sender As Object, ByVal e As System.Data.DataRowChangeEventArgs)
Select Case e.Action
Case System.Data.DataRowAction.Change
If e.Row.RowState <> System.Data.DataRowState.Unchanged Then
_changesDetected += 1
End If
End Select
End Sub
Private Function CreateTableWithData() As System.Data.DataTable
Dim newTable As New System.Data.DataTable
Dim columnID As New System.Data.DataColumn("ID", GetType(Guid))
Dim columnA As New System.Data.DataColumn("ColumnA", GetType(String))
Dim columnB As New System.Data.DataColumn("ColumnB", GetType(String))
newTable.Columns.AddRange({columnID, columnA, columnB})
newTable.PrimaryKey = {newTable.Columns(0)}
For i = 0 To 5
Dim dr = newTable.NewRow
dr("ID") = Guid.NewGuid
dr("ColumnA") = String.Format("Column A Row {0}", i.ToString)
dr("ColumnB") = String.Format("Column B Row {0}", i.ToString)
newTable.Rows.Add(dr)
Next
Return newTable
End Function
Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
End Class
So, in the MergeTables method, I make a change to the first row in _table2 and I make a change in the second row of _table1.
Because I made a change the first row in _table1, the _table1.GetChanges method returns a DataTable with all of the changed rows (just the first row in this case).
I then merge the table containing the changes with _table2 and indicate that I want to preserve changes.
Once that merge is completed I know that the results are preserving the changes that I had made before the merge and that the table will contain the new data as well (so long as there were no conflicts). The result of merging the incoming data into the changes table will resolution of any conflicts in data.
After I have that resolved table I can safely merge into the original _table1 table indicating that preserve change = false. Because passing false as the preserve changes parameter results in no RowState changes for the original data everything works perfectly fine! My changes are preserved And the RowStates are not modified!
Happy Coding!
-Frinny

Datatables left join linq

I'm having a hard time joining 2 datatables and have the joined datatable
as result.
First datatable (labels) holds data including a printerid.
Second datatable (printers) holds printer references (id > unc)
I would like to have as endresult (joined) a datatable with all data
from the first datatable with the field (unc) of the second datatable.
This is what i have been trying: (mind that fixed paths are for convenience...)
Sub Main()
Dim ds1 As new DataSet
ds1.ReadXml("C:\Program Files (x86)\[COMPANY]\ASW2XML\BICOLOR_a07bfc62-501e-4444-9b6e-3b9d3550e1a4.xml")
Dim ds2 As New DataSet
Dim li As string()
li = IO.File.ReadAllLines("C:\Program Files (x86)\[COMPANY]\ASW2XML\printers.dat")
Dim printers As New DataTable("Printers")
printers.Columns.Add("REPRT2")
printers.Columns.Add("REPRT3")
For Each s In li.ToList
Dim dr As DataRow = printers.NewRow
dr.Item(0) = s.Split("=")(0)
dr.Item(1) = s.Split("=")(1)
printers.Rows.Add(dr)
Next
printers.AcceptChanges
Dim labels As DataTable = ds1.Tables(0)
Dim joined As new DataTable("data")
'Dim lnq = From label In labels.AsEnumerable Join printer In printers.AsEnumerable On label("REPRT") Equals printer("REPRT2") Select printer
'Dim lnq = From l In labels Group Join p In printers On l Equals p("REPRT2") Into Group From p In Group Select label = l, ppath = If(p Is Nothing, "(Nothing)", p("REPRT3"))
Dim lnq = labels.AsEnumerable().Where(Function(o)printers.Select("REPRT2 =" & o.Item("REPRT").ToString).Length = 0)
joined = lnq.CopyToDataTable
End Sub
Thx for your help and inspiration!
grtz -S-
Have you tried http://msdn.microsoft.com/en-us/library/system.linq.enumerable.join.aspx
Make your datatables as an extension of IEnumerable (like list as you did) and a join in linq would make it work easily.
then send the joined tables to any destination you want.
I thought a comment would be harder to understand so I moved it in a post.
I wrote a bit of strucure that shoud help you understand how Join works:
structure Label
Public printerId as long
Public driver as Strring
end structure
structure Printer
Public unc as string
end structure
If you set Labels and Printers to DataTable (instead of the structures below), you shoud have something like :
function DoJoin() as datatable
'You might remove as datatable in query declaration
dim query as datatable = Labels.Join(Printers, Function(aLabel) aLabel, _
function(aPrinter) aPrinter.unc, _
function(aLabel, aPrinter) New With
{ .printerID = aLabel.printerId, .driver = aLabel.Driver, _
.unc = aPrinter.unc
})
return query
end function
However I wote it this morning in notepad, so you might have to adjust this. I just want to add that you must have the same type of container in order to use join (eg : example in Join at msdn they used 2 Lists.), that are compatible with Linq obects (datatable I do not know).
I decided to do it the "hard" way and loop through all rows in parent table.
This goes very fast for a minor amount of records, I don't know if running this on
a larger amount of records speed will greatly decrease over using a Linq solution...
This is the code that I'm using atm:
Sub Main()
Dim ds As new DataSet
ds.ReadXml("C:\Program Files (x86)\[COMPANY]\ASW2XML\BICOLOR_a07bfc62-501e-4444-9b6e-3b9d3550e1a4.xml")
Dim labels As DataTable = ds.Tables(0)
Dim printers As DataTable = GetPrinters
labels.Columns.Add("REPRT2").SetOrdinal(labels.Columns.IndexOf("REPRT")+1) 'insert new column after key column
labels.CaseSensitive=False
labels.AcceptChanges
For Each dr As DataRow In labels.Rows
Dim p As String = String.Empty
Try
p = printers.Select("ID='" & dr("REPRT") & "'")(0).Item("PATH").ToString
Catch ex As Exception
End Try
dr("REPRT2") = p
Next
labels.AcceptChanges
End Sub
Function GetPrinters As DataTable
Dim printers As New DataTable("Printers")
Dim li As string()
li = IO.File.ReadAllLines("C:\Program Files (x86)\[COMPANY]\ASW2XML\printers.dat")
printers.Columns.Add("ID")
printers.Columns.Add("PATH")
For Each s In li.ToList
Dim dr As DataRow = printers.NewRow
dr.Item(0) = s.Split("=")(0)
dr.Item(1) = s.Split("=")(1)
printers.Rows.Add(dr)
Next
printers.AcceptChanges
Return printers
End Function