Results 1 to 6 of 6

Thread: How to copy one row from gridview A to gridview B

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2007
    Posts
    3

    How to copy one row from gridview A to gridview B

    Hi, i am using Visual Web Developer 2005 Express, i work with Visual Basic.
    I need to copy one row from gridview A to gridview B, i have a checkbox in the first column that i let the user check it if they want to copy just one row, and if they want to copy more rows is just check more. And when the user press one button i need to loop the gridview and see what are the checked checkboxes and copy those rows to the other gridview. Any one can help me with the code. Thank you.

  2. #2
    Fanatic Member MetalKid's Avatar
    Join Date
    Aug 2005
    Location
    Green Bay, Wisconsin
    Posts
    534

    Re: How to copy one row from gridview A to gridview B

    It depends how the data is bound to grid A. If you are using a datatable, then you will have to create a new datatable and add the rows you want to show manually. Then loop thru the datatable you have bound to grid A. If the row is checked, do something like this:

    Code:
    Dim row As DataRow = NewTable.NewRow()
    For i As Int32 = 0 To NewTable.Columns.Count - 1
       row(i) = gridARow(i)
    Next
    NewTable.Rows.Add(row)
    
    ... After done looping thru GridA rows
    
    gridB.DataSource = NewTable
    If your problem is solved, please use the Mark Thread As Resolved under Thread Tools!

    Show Appreciation. Rate Posts!

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2007
    Posts
    3

    Re: How to copy one row from gridview A to gridview B

    Hi, i read your post and tried to solve my problem, but i couldn't yet.
    I have a gridview and a sqldatasource, and the select statement is

    SELECT DISTINCT p.Product_ID, [Product Name] AS Product_Name, [Sell Price], Quantity FROM Products AS p INNER JOIN Product_Categories AS pc ON p.Product_ID = pc.Product_ID WHERE (p.Product_ID LIKE '%' + @product_ID + '%') and ((pc.Category_ID LIKE @Category_ID) OR (pc.Category_ID LIKE @Category_ID2)) ORDER BY product_name

    The user just choose products to be showed by categories or brands in a dropdownlist and i invoke the databind of the sqldatasource with the above sql statement. I need to search in the gridview1 for row checked to copy the row to the gridview2. I use a templatefield called cbxselect to select the rows i want to copy. I need help, because the system show a message saying that the gridview1Row is not declared. What i am doing wrong?

    Take a look at my code:

    Protected Sub btninsert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btninsert.Click

    For Each gvr As GridViewRow In GridView1.Rows
    Dim cb As CheckBox = CType(gvr.FindControl("cbxselect"), CheckBox)

    If cb.Checked = True Then
    Dim row As DataRow = NewTable.NewRow()
    For i As Int32 = 0 To NewTable.Columns.Count - 1
    row(i) = GridView1Row(i)
    Next
    NewTable.Rows.Add(row)

    End If
    Next
    ''... After done looping thru GridA rows

    GridView2.DataSource = NewTable
    End Sub

  4. #4
    Fanatic Member MetalKid's Avatar
    Join Date
    Aug 2005
    Location
    Green Bay, Wisconsin
    Posts
    534

    Re: How to copy one row from gridview A to gridview B

    Something like:

    row(NewTable.Columns(i).ColumnName) = gvr(NewTable.Columns(i).ColumnName)
    If your problem is solved, please use the Mark Thread As Resolved under Thread Tools!

    Show Appreciation. Rate Posts!

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2007
    Posts
    3

    Re: How to copy one row from gridview A to gridview B

    I am still with doubts inside the loop, how can i put the value of each column in each row in the other gridview. Lets say i have 2 columns called product_id and product name, how can i do this. I have highlighted the code here i have doubts. Please help me.

    look:

    For Each gvr As GridViewRow In GridView1.Rows
    Dim cb As CheckBox = CType(gvr.FindControl("cbxselect"), CheckBox)

    If cb.Checked = True Then
    For i As Int32 = 0 To GridView1.Columns.Count - 1 ---is this correct for loop to every column in Row(i)?


    ' Here is my problem how can i copy the column from gridview source to gridview Destination

    gridviewDestination.column(i)= gridviewsource.column(i) --- is this correct? what is the correct syntax to do this?

    Next

    End If
    Next

  6. #6
    Fanatic Member MetalKid's Avatar
    Join Date
    Aug 2005
    Location
    Green Bay, Wisconsin
    Posts
    534

    Re: How to copy one row from gridview A to gridview B

    Here is the whole thing:

    Code:
    Dim data as DataTable = HoweverYouGetYourData()
    Dim newTable as New DataTable
    For i as Int32 = 0 to data.Columns.Count - 1
       newTable.Columns.Add(data.Columns(i).ColumnName)
    Next
    
    For Each gvr As GridViewRow In GridView1.Rows
    Dim cb As CheckBox = CType(gvr.FindControl("cbxselect"), CheckBox)
    
    If cb.Checked = True Then
       Dim row as DataRow = newTable.NewRow
       For i as Int32 = 0 to data.Columns.Count - 1
          row(data.Columns(i).ColumnName) = gvr.Cells(data.Columns(i).ColumnName).Value
       Next
       newTable.Rows.Add(row)
    End If
    
    Next
    
    GridView2.DataSource = newTable
    If your problem is solved, please use the Mark Thread As Resolved under Thread Tools!

    Show Appreciation. Rate Posts!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width