Add selected items with checkobx in a gridview to a textbox

0

I have a Gridview where I have several columns, the last one has numeric values, my gridiview poses a checkbox, what I want to do is to do a check in one or more rows, add the amounts to a texbox, try several ways as you will see low but I could not get it, I hope you can help me help

Button code:

Protected Sub BtnAplica_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnAplica.Click

    Dim resultado As String
    Dim resultado2 As String
    Dim Seleccion As Boolean = False
    Dim sum As Double = 0




    For Each RW As GridViewRow In Gvcobranzas.Rows

        If CType(RW.Cells(2).FindControl("CheckBox1"), CheckBox).Checked Then
            If GM.ValidaCliente(TxtBuscarCli.Text) = False Then
            ElseIf GM.ValidaNPago(TxtNuPago.Text) = False Then
                resultado = GM.IsnPago(TxtBuscarCli.Text, dlBnaco.SelectedValue, TxtPagoTotal.Text, TxtNuPago.Text, TxtFePago.Text, TxtFeReg.Text)
                Seleccion = True
                With lberror1
                    .Visible = True
                    .Text = resultado
                End With
                Limpiar()
                CargaGridClientes()
                CargaGridnotascreditos()
            Else
                With lberror1
                    .Visible = True
                    .Text = "Ya existe un numero de pago, no se permiten duplicados"
                End With
                Limpiar()
                CargaGridClientes()
                CargaGridnotascreditos()
            End If

        Else
            With lberror1
                .Visible = True
                .Text = "Favor de seleccionar las factura a pagar"
            End With
            Limpiar()
        End If
    Next
    For Each fila As GridViewRow In GvNotasCredito2.Rows
        Dim NCF As String = fila.Cells(1).Text
        Dim monto As Double = fila.Cells(2).Text
        resultado2 = GM.InsNotaC(NCF, TxtNuPago.Text, monto)
        With lberror0
            .Visible = True
            .Text = resultado2
        End With


    Next
    CargaGridClientes()
    CargaGridnotascreditos()
End Sub

I tried to do this method but it does not work for me

Private Sub SumOpcion2()

   Dim total As Decimal = 0
   For Each row As GridViewRow In Gvcobranzas.Rows
       If CType(row.Cells(0).FindControl("CheckBox1"), CheckBox).Checked Then
           Dim x As Decimal = 0
           If Decimal.TryParse(row.Cells(0).Text, x) Then
               total += x
           End If
       End If
   Next
  TxtMonto.Text = total
End Sub

even try the Changed CheckBox event but not

Protected Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    For Record As Integer = 0 To (Gvcobranzas.Rows.Count - 1)
        CType(Gvcobranzas.Rows(Record).FindControl("CheckBox1"), CheckBox).Checked = CType(Gvcobranzas.HeaderRow.FindControl("CheckBox1"), CheckBox).Checked
    Next
    Dim total As Decimal = 0
    For Each row As GridViewRow In Gvcobranzas.Rows
        If CType(row.Cells(0).FindControl("CheckBox1"), CheckBox).Checked Then
            Dim x As Decimal = 0
            If Decimal.TryParse(row.Cells(0).Text, x) Then
                total += x
            End If
        End If
    Next
    TxtMonto.Text = total
End Sub
    
asked by dante 23.08.2017 в 20:14
source

1 answer

0

I managed to do it by javascript

                $(function() {
                     //event handler to the checkbox selection change event
                     $("input[type=checkbox]").change(function() {
                         //variables to store the total price of selected rows
                         //and to hold the reference to the current checkbox control
                         var totalPrice = 0, ctlPrice;
                         //iterate through all the rows of the gridview
                         $('.Gvcobranzas tr').each(function() {
                             //if the checkbox in that rows is checked, add price to our total proce
                             if ($(this).find('input:checkbox').attr("checked")) {
                                 ctlPrice = $(this).find('[id$= Label3]');
                                 //since it is a currency column, we need to remove the $ sign and then convert it
                                 //to a number before adding it to the total
                                 totalPrice += parseFloat(ctlPrice.text().replace(/[^\d\.]/g, ''));
                             }
                         });
                         //finally set the total price (rounded to 2 decimals) to the total paragraph control.
                         $('.sum').text("$ " + totalPrice.toFixed(2));
                     });
                 });
    
answered by 25.08.2017 в 22:45