locked
Gridview data RRS feed

  • Question

  • User639567535 posted

    I try to transfer data from oneGridView to another,GridView

    by clicking on button.

    I fill GridView

    like this:

    Function Getdatas() As DataTable
    
        Dim parameter() As String = {"ID"}
        Dim valuespara() As Object = {DROPDOWN1.SelectedValue}
    
        databaestructure= datasetobject.GridFill(gridview1, "tbl", "spdata", parameter,valuespara)
    
    End Function

    And clicking on a button, my GridView

    is displayed:

    Protected Sub showgrid_click(ByVal sender As Object, ByVal e As System.EventArgs) Handles showgrid_click.Click
        Getdatas()
    End Sub

    Now on that button, when I click transfer then whole data should transfer to another GridView

    .

    I do this:

    Protected Sub copydata_click(ByVal sender As Object, ByVal e As System.EventArgs) Handles copydata_click.Click
        For i As Integer = 0 To Me.gridview1.Rows.Count - 1
            GridView2.Rows.Add(gridview1.Rows(i).Cells(0).Value)
        Next
    End Sub

    This shows an error however:

    'Add' is not a member of 'System.Web.UI.WebControls.GridViewRowCollection'.

    'Value' is not a member of 'System.Web.UI.WebControls.TableCell'.

    How I do this?

    Thursday, May 18, 2017 12:19 PM

All replies

  • User632428103 posted

    Hello capricon user,

    looks at this article very nice to follow in c:# or vb :

    http://codedisplay.com/transfer-records-from-one-gridview-to-another-gridview-asp-net-c/

    nice sample

    Thursday, May 18, 2017 2:01 PM
  • User-1716253493 posted

    Afaik, there no ADD method in gridview like datatable, you can't add new row to the grid.

    Add new row to the datasource then rebind the grid.

    maybe you can store a datatable in a session or  viewstate

    Add new row to the datatable then rebind the grid.

    Friday, May 19, 2017 2:03 AM
  • User639567535 posted
    Dim dtt As New DataTable()
    
        For Each cell As TableCell In GridView1.HeaderRow.Cells
            dtt.Columns.Add(cell.Text)
        Next
    
        For Each row As GridViewRow In GridView1.Rows
            dtt.Rows.Add()
            For i As Integer = 0 To row.Cells.Count - 1
                dtt.Rows(row.RowIndex)(i) = row.Cells(i).Text
    
            Next
        Next
    
        GridView2.DataSource = dtt
        GridView2.DataBind()

    but this shows an error

    there is 2 header text with text "Amount "

    A column named 'Amount' already belongs to this DataTable.
    Friday, May 19, 2017 7:37 AM
  • User-271186128 posted

    Hi Capricon User,

    I suggest you could refer to the following code to loop through the GridView, and convert it to datatable, then bind it to another GridView.

      Protected Sub Button1_Click(sender As Object, e As EventArgs)
    
            Dim table = GetDataTable(GridView2)
    
            GridView5.DataSource = table
            GridView5.DataBind()
    
        End Sub
    
    
    
        Private Function GetDataTable(dtg As GridView) As DataTable
            Dim dt As New DataTable()
    
            ' add the columns to the datatable            
            If dtg.HeaderRow IsNot Nothing Then
                For i As Integer = 0 To dtg.HeaderRow.Cells.Count - 1
    
                    dt.Columns.Add(dtg.HeaderRow.Cells(i).Text)
                Next
            End If
    
            '  add each of the data rows to the table
            For Each row As GridViewRow In dtg.Rows
                Dim dr As DataRow
                dr = dt.NewRow()
    
                For i As Integer = 0 To row.Cells.Count - 1
                    dr(i) = row.Cells(i).Text.Replace(" ", "")
                Next
                dt.Rows.Add(dr)
            Next
            Return dt
        End Function
    

    From my point of view, I think the easiest way is that you could use Session or ViewState to store the GridView DataSource when you bind the first GridView, then, in the button click event, you could get the data source from Session, and rebind another GridView.

    Best regards,
    Dillion

    Friday, May 19, 2017 7:45 AM
  • User-271186128 posted

    Hi Capricon User,

    A column named 'Amount' already belongs to this DataTable.

    As for this issue, before add new columns into the datatable, you could use the DataTable.Columns.Contains method to check whether the column is exist.

    Code like this:

            ' add the columns to the datatable            
            If dtg.HeaderRow IsNot Nothing Then
                For i As Integer = 0 To dtg.HeaderRow.Cells.Count - 1
                    If dt.Columns.Contains(dtg.HeaderRow.Cells(i).Text) Then
                        dt.Columns.Add(dtg.HeaderRow.Cells(i).Text + i.ToString())
                    Else
                        dt.Columns.Add(dtg.HeaderRow.Cells(i).Text)
                    End If
    
                Next
            End If

    Best regards,
    Dillion

    Friday, May 19, 2017 7:52 AM
  • User639567535 posted

    this shows same error

    beacuse there is 2 header text with same name

    Friday, May 19, 2017 8:00 AM
  • User-1716253493 posted

    this shows same error

    beacuse there is 2 header text with same name

                If dtt.Columns.Contains(cell.Text) Then
                    dtt.Columns.Add(cell.Text & "1")
                Else
                    dtt.Columns.Add(cell.Text)
                End If

    Friday, May 19, 2017 8:18 AM