Tengo dos DataGridView
las siguientes columnas:
- CODIGO
- NOMBRE
- CANTIDAD.
A través de TextBox
y un botón
, estoy pasando valores de un DataGridView
a otro, agregando nuevas filas.
¿Es posible lograr que al encontrar valores idénticos en CODIGO
y NOMBRE
, aumente la CANTIDAD
, actualizando esa fila, sin crear una nueva?
Este es mi código:
Dim newRow As DataRowView = DirectCast(Pedidos_GeneralBindingSource.AddNew(), DataRowView)
newRow("CODIGO") = CODIGOTextBox.Text
newRow("NOMBRE") = NOMBRETextBox.Text
newRow("CANTIDAD") = CANTIDADTextBox.Text
EDITADO
Este es el código que tengo hasta ahora:
Dim DT_Segundo As New DataTable
DT_Segundo.Columns.Add("CODIGO", Type.[GetType]("System.String"))
DT_Segundo.Columns.Add("NOMBRE", Type.[GetType]("System.String"))
DT_Segundo.Columns.Add("CANTIDAD", Type.[GetType]("System.String"))
Dim primarykey(1) As DataColumn
primarykey(0) = DT_Segundo.Columns("CODIGO")
DT_Segundo.PrimaryKey = primarykey
DataGridView2.DataSource = DT_Segundo
Dim CODIGOi As String
Dim NOMBREi As String
Dim CANTIDADi As String
CODIGOi = CODIGOTextBox.Text
NOMBREi = NOMBRETextBox.Text
CANTIDADi = CANTIDADTextBox.Text
If Not DT_Segundo.Rows.Find(CODIGOi) Is Nothing Then
DT_Segundo.Rows.Find(CODIGOi).Item("CANTIDAD") = DT_Segundo.Rows.Find(CODIGOi).Item("CANTIDAD") + CANTIDADi
Else
DT_Segundo.Rows.Add(CODIGOi, NOMBREi, CANTIDADi)
End If
Mi problema está ahora en que al ejecutarlo, reemplazo los valores existentes de mi DataGridView2 por una fila creada a partir de los valores alojados en los TextBox. No agrega nuevas filas ni suma la cantidad para aquellas que tienen mismo valor en la columna "CODIGO".
EDITADO 2
Agregue la columna COSTO para calcular valor y finalmente pude lograrlo con otro código, lo dejo para aquel que le pueda servir:
Dim Found As Boolean = False
Dim total As Double = Convert.ToDouble(COSTOTextBox.Text) * 1
If Pedidos_GeneralDataGridView.Rows.Count > 0 Then
For Each row As DataGridViewRow In Pedidos_GeneralDataGridView.Rows
If Convert.ToString(row.Cells(0).Value) = CODIGOTextBox.Text Then
row.Cells(2).Value = Convert.ToString(1 + Convert.ToInt16(row.Cells(2).Value))
row.Cells(3).Value = Convert.ToDouble(row.Cells(3).Value) + Convert.ToDouble(COSTOTextBox.Text)
Found = True
End If
Next
If Not Found Then
Dim newRow As DataRowView = DirectCast(Pedidos_GeneralBindingSource.AddNew(), DataRowView)
newRow("CODIGO") = CODIGOTextBox.Text
newRow("NOMBRE") = NOMBRETextBox.Text
newRow("CANTIDAD") = CANTIDADTextBox.Text
newRow("COSTO") = COSTOTextBox.Text
End If
End If