locked
Add value from checkbox to datatable RRS feed

  • Question

  • User-1371397710 posted

    Hi, I'm trying to adapt the following code to my needs :

    Sub AddToCart(ByVal s As Object, ByVal e As EventArgs)
            
    objDT = Session("Cart")
        
            Dim Prod As String
            Dim blnMatch As Boolean = False
    
            Prod = ddlProdotto.SelectedItem.Text
    
            For Each objDR In objDT.Rows
                If objDR("Product") = Prod Then
                    objDR("Quantity") += txtQuantity.Text
                    blnMatch = True
                    Exit For
                End If
            Next
    
            If Not blnMatch Then
                objDR = objDT.NewRow
                objDR("Quantity") = txtQuantity.Text
                objDR("Product") = ddlProdotto.SelectedItem.Text
                objDR("UM") = ddlUM.SelectedItem.Text
    
                objDT.Rows.Add(objDR)
            End If
      
    
            Session("Cart") = objDT
    
            dg.DataSource = objDT
            dg.DataBind()


     

    Now, I add product to cart from a dropdownlist value.
    I would rather that the product be added to the "cart" when it is selected by checkbox, only that I have no idea how to do

    I create a GridView called GridView1 and then in the gridview I create a checkbox.

    the line, I think, in question is:
     
    objDR ( "Product") = ddlProdotto.SelectedItem.Text

     

    Thursday, February 25, 2010 10:30 AM

Answers

  • User-2119480821 posted

    try like this..

    Sub AddToCart(ByVal s As Object, ByVal e As EventArgs)
      objDT = Session("Cart")
      Dim blnMatch As Boolean = False
      Dim i As Integer
      Dim cb As CheckBox
           
      For i = 0 To GridView1.Rows.Count - 1
        cb = DirectCast(GridView1.Rows(i).FindControl("chkSelect"), CheckBox)
        If cb.Checked Then
         objDR = objDT.NewRow
         objDR("Code") = GridView1.DataKeys(i).Values(0)
         objDR("Product") = GridView1.DataKeys(i).Values(1)
         objDR("Quantity") = txtQuantity.Text
         objDR("UM") = ddlUM.SelectedItem.Text
         objDT.Rows.Add(objDR)
        End If
     Next
    
     For Each objDR In objDT.Rows
        For i = 0 To GridView1.Rows.Count - 1
            If objDR("Code") = GridView1.DataKeys(i).Value(0) Then
               objDR("Quantity") += txtQuantity.Text
               blnMatch = True
               Exit For
            End If
        Next       
     Next
    
     Session("Cart") = objDT
     dg.DataSource = objDT
     dg.DataBind()


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 1, 2010 10:32 AM
  • User874198490 posted

    Sorry I was confused with another programming language

    The line shoulde be:

    If objDR Is Nothing Then

    ....

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 1, 2010 1:37 PM

All replies

  • User1324895001 posted

    I know my code is in C#, but might help with your question and get you closer to a solution. I have a gridview that has radiobuttons and i build a datalist with the selections made. The portions in bold may help you adjust your code.

        protected void btnNotes_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("ID", typeof(int)));
            dt.Columns.Add(new DataColumn("STAT", typeof(string)));
            foreach (GridViewRow row in GridView1.Rows)
            {
                RadioButtonList cbl = (RadioButtonList)row.FindControl("results");
                    if ((cbl.SelectedValue == "BKOK") || (cbl.SelectedValue == "BKNO"))
                    {
                        DataRow dr = dt.NewRow();
                        dr["ID"] = GridView1.DataKeys[row.RowIndex].Value;
                        dr["STAT"] = cbl.SelectedValue.ToString();
                        dt.Rows.Add(dr);
                    }
                    DataList1.DataSource = dt;
                    DataList1.DataBind();
            }
            btnUpdate.Visible = true;
        }


     

    Thursday, February 25, 2010 11:57 AM
  • User-1371397710 posted

    Hi, thanks for reply

    I modified in :

       If Not blnMatch Then
    
                For i = 0 To GridView1.Rows.Count - 1
                    Dim cb As CheckBox = DirectCast(GridView1.Rows(i).FindControl("chkSelect"), CheckBox)
    
                    If cb.Checked Then
                        objDR = objDT.NewRow
                        objDR("Quantity") = txtQuantity.Text
                        objDR("Product") =  ??????????????
                        objDR("UM") = ddlUM.SelectedItem.Text
    
                        objDT.Rows.Add(objDR)
                    End If
                Next
    
            End If


    but I do not know what to put in :

    objDR("Product") =  

    because GridView1.DataKeys[row.RowIndex].Value is not allowed Cry
    Friday, February 26, 2010 4:10 AM
  • User-1371397710 posted

    I made the changes I needed.
    In the gridview I added two "DataKeys" (Code, ProductName)


    It works, but it remains the problem to avoid duplicate values. The code that I tried to fix does not work
    If I post twice for the same product, rather than the total quantity, it puts me two rows in the table.

    Here's the code I used:

    Sub AddToCart(ByVal s As Object, ByVal e As EventArgs)
    objDT = Session("Cart")

    Dim blnMatch As Boolean = False
    Dim i As Integer
    Dim cb As CheckBox

    If Not blnMatch Then

    For i = 0 To GridView1.Rows.Count - 1

    cb = DirectCast(GridView1.Rows(i).FindControl("chkSelect"), CheckBox)

    ' IT SHOULD AVOID DUPLICATES VALUES
    For Each objDR In objDT.Rows
    If objDR("Code") = GridView1.DataKeys(i).Value(0) Then
    objDR("Quantity") += txtQuantity.Text
    blnMatch = True
    Exit For
    End If
    Next

    If cb.Checked Then
    objDR = objDT.NewRow
    objDR("Code") = GridView1.DataKeys(i).Values(0)
    objDR("Product") = GridView1.DataKeys(i).Values(1)
    objDR("Quantity") = txtQuantity.Text
    objDR("UM") = ddlUM.SelectedItem.Text

    objDT.Rows.Add(objDR)

    End If

    cb.Checked = False
    Next


    End If


    Session("Cart") = objDT

    dg.DataSource = objDT
    dg.DataBind()


    Sunday, February 28, 2010 3:49 PM
  • User-1371397710 posted

    Sub AddToCart(ByVal s As Object, ByVal e As EventArgs)
            objDT = Session("Cart")
            Dim blnMatch As Boolean = False
            Dim i As Integer
            Dim cb As CheckBox
            If Not blnMatch Then
                For i = 0 To GridView1.Rows.Count - 1
                    cb = DirectCast(GridView1.Rows(i).FindControl("chkSelect"), CheckBox)
                    For Each objDR In objDT.Rows
                        If objDR("Code") = GridView1.DataKeys(i).Value(0) Then
                            objDR("Quantity") += txtQuantity.Text
                            blnMatch = True
                            Exit For
                        End If
                    Next
                    If cb.Checked Then
                        objDR = objDT.NewRow
                        objDR("Code") = GridView1.DataKeys(i).Values(0)
                        objDR("Product") = GridView1.DataKeys(i).Values(1)
                        objDR("Quantity") = txtQuantity.Text
                        objDR("UM") = ddlUM.SelectedItem.Text
                        objDT.Rows.Add(objDR)
                    End If
                    cb.Checked = False
                Next
            End If

            Session("Cart") = objDT
            dg.DataSource = objDT
            dg.DataBind()

    Sunday, February 28, 2010 3:51 PM
  • User-1371397710 posted

    someone who can help me? 

    Monday, March 1, 2010 8:26 AM
  • User-343876356 posted

    objDR("Product") =  ?????????????? will objDR("Product") =  cb.Value

    If u r using  asp:CheckBoxList then use this

    If cb.Selected Then 
             objDR = objDT.NewRow  
             objDR("Quantity") = txtQuantity.Text  
             objDR("Product") =  cb.Value

             objDR("UM") = ddlUM.SelectedItem.Text  
             objDT.Rows.Add(objDR)  
    End If

     

    otherwise u can use this

    If cb.Selected Then 
             objDR = objDT.NewRow  
             objDR("Quantity") = txtQuantity.Text  
             objDR("Product") =  cb.Text

             objDR("UM") = ddlUM.SelectedItem.Text  
             objDT.Rows.Add(objDR)  
    End If

    Monday, March 1, 2010 8:58 AM
  • User-343876356 posted

    post your code so that anyone can help you out. 

    Monday, March 1, 2010 9:13 AM
  • User-1371397710 posted

    I apologize, perhaps I failed to explain myself

    The part that I do not know how to do (in bold), is to sum quantity if I add more times the same product. Because, at the moment, if I enter the same product several times it creates separate lines

    For Each objDR In objDT.Rows
                If objDR("Code") = GridView1.DataKeys(i).Values(0) Then
                    'If objDR("Code") = GridView1.SelectedRow.Cells(0).Text Then
                    objDR("Quantity") += txtQuantity.Text
                    blnMatch = True
                End If
                Exit For
            Next
            If Not blnMatch Then
                For i = 0 To GridView1.Rows.Count - 1
                    cb = DirectCast(GridView1.Rows(i).FindControl("chkSelect"), CheckBox)
                    If cb.Checked Then
                        objDR = objDT.NewRow
                        objDR("Code") = GridView1.DataKeys(i).Values(0)
                        objDR("Product") = GridView1.DataKeys(i).Values(1)
                        objDR("Quantity") = txtQuantity.Text
                        objDR("UM") = ddlUM.SelectedItem.Text
                        objDT.Rows.Add(objDR)
                 End If
                    cb.Checked = False
                Next


     

    Monday, March 1, 2010 9:41 AM
  • User874198490 posted

    If you have a primary key field in the table then you can use the DataSet.Tables("TableName").Rows.Find function to check if the product is already in the cart.

    Monday, March 1, 2010 9:47 AM
  • User874198490 posted

    To be more specific:

            For i = 0 To GridView1.Rows.Count - 1
                cb = DirectCast(GridView1.Rows(i).FindControl("chkSelect"), CheckBox)
                If cb.Checked Then
                    objDR = objDT.Rows.Find(GridView1.DataKeys(i).Values(0))
                    If objDR = nil Then
                        objDR = objDT.NewRow
                        objDR("Code") = GridView1.DataKeys(i).Values(0)
                        objDR("Product") = GridView1.DataKeys(i).Values(1)
                        objDR("Quantity") = txtQuantity.Text
                        objDR("UM") = ddlUM.SelectedItem.Text
                        objDT.Rows.Add(objDR)
                    Else
                        objDR("Quantity") += Convert.ToInt16(txtQuality.Text)
                    End If
                    cb.Checked = False
            Next
                              

    If you don't want or have the "code" field you can use the iteration you had before instead, but you have to place it within the for I= ... next iteration


    Ger

    Monday, March 1, 2010 10:21 AM
  • User-2119480821 posted

    try like this..

    Sub AddToCart(ByVal s As Object, ByVal e As EventArgs)
      objDT = Session("Cart")
      Dim blnMatch As Boolean = False
      Dim i As Integer
      Dim cb As CheckBox
           
      For i = 0 To GridView1.Rows.Count - 1
        cb = DirectCast(GridView1.Rows(i).FindControl("chkSelect"), CheckBox)
        If cb.Checked Then
         objDR = objDT.NewRow
         objDR("Code") = GridView1.DataKeys(i).Values(0)
         objDR("Product") = GridView1.DataKeys(i).Values(1)
         objDR("Quantity") = txtQuantity.Text
         objDR("UM") = ddlUM.SelectedItem.Text
         objDT.Rows.Add(objDR)
        End If
     Next
    
     For Each objDR In objDT.Rows
        For i = 0 To GridView1.Rows.Count - 1
            If objDR("Code") = GridView1.DataKeys(i).Value(0) Then
               objDR("Quantity") += txtQuantity.Text
               blnMatch = True
               Exit For
            End If
        Next       
     Next
    
     Session("Cart") = objDT
     dg.DataSource = objDT
     dg.DataBind()


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 1, 2010 10:32 AM
  • User-1371397710 posted

     thanks Gerrievs, it might be the right way

    But I do not understand the line :

    If objDR = nil Then  

    nil means  ?

     

    Monday, March 1, 2010 10:40 AM
  • User874198490 posted

    Sorry I was confused with another programming language

    The line shoulde be:

    If objDR Is Nothing Then

    ....

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 1, 2010 1:37 PM
  • User-1371397710 posted

    You are great Kiss

    It Works Tongue out 

    Tuesday, March 2, 2010 8:50 AM