Hi Maam and Sir.
I need your expertise on vb.net.
I have two data grid with database on their own. I want to copy/update the data from one database to another.
The data grid have product codes and prices. I want to copy/update only the prices from the first data grid or vice versa.
Also I have checked boxes so i can select more to copy or update.
Please help me. I attached the screenshot of my data grid.
Thank you.
rproffitt 2,662 "Nothing to see here." Moderator
Here's the deal. vb.net can use quite a few databases so you need to share more. My last setup was for MySQL for the database server since we wanted to scale up.
Your question is also, not one but a few. Break it down to a few questions along with what database you are using.
-> As to updating one database from another, here that's a spin on the old question I placed in this google.
https://www.google.com/search?q=sql+update+rows+from+another+database
Now about your checkmark version, that would not be the update from another database by code written by you to respond to the button click and do the work. In otherwords, another question as I read your post.
Rica_1 0 Newbie Poster
@rproffitt.
Hi Sir. I use MySql for the database. I just want the checked items from one datagrid(dbFirst) to be updated to another datagrid(dbSecond). They have the same product codes. I just want the price to be updated when I select the checked boxes and click the >> button.
Thank you Sir for the immediate response. :)
rproffitt 2,662 "Nothing to see here." Moderator
Then you need to write code to respond to your button click. Said code will find what was clicked and update just those rows. This is your code to write.
I do know folk that make screens like yours which doesn't take much code at all if any but to get what you want done, it's time to write code.
xrjf 213 Posting Whiz
If product names are really the same in both datagridview, i.e., no need to add rows:
Dim us As New Globalization.CultureInfo("en-US")
Function parseN(value As String, ByRef result As Double) As Boolean
Return Double.TryParse(value, Globalization.NumberStyles.Any, us, result)
End Function
Private Sub btnCopyLeftToRight_Click(sender As System.Object, e As System.EventArgs) Handles btnCopyLeftToRight.Click
CopyDGV(DataGridViewLeft, DataGridViewRight)
End Sub
Private Sub btnCopyRightToLeft_Click(sender As System.Object, e As System.EventArgs) Handles btnCopyRightToLeft.Click
CopyDGV(DataGridViewRight, DataGridViewLeft)
End Sub
Sub CopyDGV(source As DataGridView, destination As DataGridView)
Try
With source
Dim colChk As Int32 = -1
For col As Int32 = 0 To .Columns.Count - 1
If .Columns(col).GetType Is GetType(DataGridViewCheckBoxColumn) Then
colChk = col
Exit For
End If
Next
If colChk = -1 Then
MessageBox.Show("Found no checkbox column.")
Exit Sub
End If
Dim vcolNames() As String = {"p_code", "deal_price", "unit_price", "unit_amt"}
Dim vcolIndex() As Int32 = {-1, -1, -1, -1}
For col As Int32 = 0 To .Columns.Count - 1
Dim colName As String = LCase(.Columns(col).Name)
Dim pos As Int32 = Array.IndexOf(vcolNames, colName)
If pos > -1 Then
vcolIndex(pos) = col
End If
Next
For i As Int32 = 0 To vcolIndex.Length - 1
If vcolIndex(i) = -1 Then
MessageBox.Show("Found no source column for " + vcolNames(i))
Exit Sub
End If
Next
For row As Int32 = 0 To .Rows.Count - 1
Dim chk As DataGridViewCheckBoxCell = .Rows(row).Cells(colChk)
If chk.Value Then
' verify p_code:'
Dim pcode = .Rows(row).Cells(vcolIndex(0)).Value
If pcode Is DBNull.Value Then
MessageBox.Show("Source p_code is null at line #" + (row + 1).ToString)
Exit Sub
End If
Dim pCodeB = destination.Rows(row).Cells(vcolIndex(0)).Value
If pCodeB Is DBNull.Value Then
MessageBox.Show("Destination p_code is null at line #" + (row + 1).ToString)
Exit Sub
End If
If pcode <> pCodeB Then
MessageBox.Show("Source and dest. p_code differ at line #" + (row + 1).ToString)
Exit Sub
End If
Dim dealp, uprice, uamt As Double
With .Rows(row)
If Not parseN(.Cells(vcolIndex(1)).Value, dealp) OrElse _
Not parseN(.Cells(vcolIndex(2)).Value, uprice) OrElse _
Not parseN(.Cells(vcolIndex(3)).Value, uamt) Then
MessageBox.Show("Error parsing numbers at line #" + (row + 1).ToString)
Exit Sub
End If
End With
destination.Rows(row).Cells(vcolIndex(1)).Value = dealp.ToString(us)
destination.Rows(row).Cells(vcolIndex(2)).Value = uprice.ToString(us)
destination.Rows(row).Cells(vcolIndex(3)).Value = uamt.ToString(us)
End If
Next
End With
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Edited by xrjf
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.