Update cell if one of the fields is equal when adding rows from a TextBox

0

I have two DataGridView the following columns:

  • CODE
  • NAME
  • AMOUNT.

Through TextBox and% botón , I'm passing values from DataGridView to another, adding new rows.

Is it possible to achieve that when finding identical values in CODIGO and NOMBRE , increase the CANTIDAD , updating that row, without creating a new one?

This is my code:

Dim newRow As DataRowView = DirectCast(Pedidos_GeneralBindingSource.AddNew(), DataRowView)

newRow("CODIGO") = CODIGOTextBox.Text
newRow("NOMBRE") = NOMBRETextBox.Text
newRow("CANTIDAD") = CANTIDADTextBox.Text

EDITED

This is the code I have so far:

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

My problem now is that when I run it, I replace the existing values of my DataGridView2 with a row created from the values stored in the TextBox. Do not add new rows or add the amount for those that have the same value in the "CODE" column.

EDITED 2

Add the COST column to calculate value and finally I could achieve it with another code, I leave it for the one that can serve:

    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
    
asked by Luciano Minetti 03.05.2017 в 21:22
source

2 answers

0

The code to create the data source for the second DataGridView and assign it to it.

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.Int32"))
DT_Segundo.PrimaryKey = New DataColumn() {DT_Segundo.Columns("Codigo"), DT_Segundo.Columns("Nombre")}
DataGridView2.DataSource = DT_Segundo

Once you have that, what you should do in the code portion you shared is:

Dim CodigoIngresado as String
Dim NombreIngresado as String
Dim CantidadIngresada As String

CodigoIngresado = CODIGOTextBox.Text
NombreIngresado = NOMBRETextBox.Text
CantidadIngresada = CANTIDADTextBox.Text 

If Not DT_Segundo.Rows.Find({CodigoIngresado,NombreIngresado}) Is Nothing Then
DT_Segundo.Rows.Find({CodigoIngresado,NombreIngresado}).Item("Cantidad") = DT_Segundo.Rows.Find({CodigoIngresado,NombreIngresado}).Item("Cantidad") + CantidadIngresada 
Else
DT_Segundo.Rows.Add(CodigoIngresado,NombreIngresado,CantidadIngresada)
End If

In this way, and thanks to defining the column Code and the column Name as Primary Key, we can find a row that already exists with the code and name to add, and if so add the amount indicated.

EDIT : (Based on more data provided in comments)

After the .Fill that you comment, add the line of code that defines the double primary key:

Me.Pedidos_GeneralTableAdapter.Fill(Me.PedidosDataSet.Pedido‌​s_General) 
 PedidosDataSet.Pedido‌​s_General.PrimaryKey = New DataColumn() {PedidosDataSet.Pedido‌​s_General.Columns("Codigo"), PedidosDataSet.Pedido‌​s_General.Columns("Nombre")}
DataGridView2.DataSource = PedidosDataSet.Pedido‌​s_General

Then where we do the verification we have left:

Dim CodigoIngresado as String
Dim NombreIngresado as String
Dim CantidadIngresada As String

CodigoIngresado = CODIGOTextBox.Text
NombreIngresado = NOMBRETextBox.Text
CantidadIngresada = CANTIDADTextBox.Text 

If Not PedidosDataSet.Pedido‌​s_General.Rows.Find({CodigoIngresado,NombreIngresado}) Is Nothing Then
PedidosDataSet.Pedido‌​s_General.Rows.Find({CodigoIngresado,NombreIngresado}).Item("Cantidad") = PedidosDataSet.Pedido‌​s_General.Rows.Find({CodigoIngresado,NombreIngresado}).Item("Cantidad") + CantidadIngresada 
Else
PedidosDataSet.Pedido‌​s_General.Rows.Add(CodigoIngresado,NombreIngresado,CantidadIngresada)
End If
    
answered by 03.05.2017 / 22:49
source
0

I recommend you first go through the rows you have in the DataGridView that you are using (it is not very clear in your question this point) , and compare if there is a row that has the same values that have your text fields.

The following pseudo-code illustrates my explanation:

  

NOTE: Do not copy and paste the code, understand it first and adjust your   code.

// Obtén los valores de los campos de texto
Dim txtCodigo As String = CODIGOTextBox.Text
Dim txtNombre As String = NOMBRETextBox.Text
Dim txtCantidad As Double = Double.Parse(CANTIDADTextBox.Text)

// Recorre las filas de tu DataGridView1 (o el que estés usando).
Foreach (fila in DataGridView1)
BeginForeach
   // Si esta fila tiene el código y el nombre iguales a los valores
   // de los textboxes...
   if (fila["CODIGO"] == txtCodigo && fila["NOMBRE"] == txtNombre)
   BeginIf
       // Actualiza la CANTIDAD:
       // Edit: Sumar la cantidad de la fila con el valor del TextBox:
       fila["CANTIDAD"] += txtCantidad
   EndIf
EndForeach
    
answered by 03.05.2017 в 22:15