How do I copy certain rows from 1 datagrid to another?

Posted on 2006-05-11
Last Modified: 2010-04-23
VB 2003 .NET
I have an already populated DATAGRID (1)
I need to copy select rows (based on criterial in a cell) to another DATAGRID (2)
How is that programed.
Example please?
Question by:yyuubum2
    LVL 28

    Expert Comment

    Here are some code snippets. They assume you have dropped two datagrids on a
    form, and have set the DataSource of the first grid to a DataTable. (The
    DataTable is important as the code assumes the first grid is bound directly
    to a DataTable.)

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
            Me.SqlDataAdapter1.Fill(Me.DataSet11) ' file the datatable for grid1

            Dim target As New DataTable("TargetTable")

            Dim source As DataTable = Me.DataGrid1.DataSource

            Dim dc As DataColumn

            For Each dc In source.Columns
                Dim dc1 As DataColumn = New DataColumn(dc.ColumnName + "_",
    dc.DataType, dc.Expression)

            target.DefaultView.AllowNew = False
            Me.DataGrid2.DataSource = target

        End Sub

        Private Sub DataGrid1_MouseUp(ByVal sender As Object, ByVal e As
    System.Windows.Forms.MouseEventArgs) Handles DataGrid1.MouseUp
            If e.Button = MouseButtons.Left Then
                Dim dr As DataRow = CType(Me.DataGrid1.DataSource,
                Dim drTarget As DataRow = CType(Me.DataGrid2.DataSource,
                Dim i As Integer
                For i = 0 To dr.ItemArray.GetLength(0) - 1
                    drTarget(i) = dr(i)
                CType(Me.DataGrid2.DataSource, DataTable).Rows.Add(drTarget)
            End If
        End Sub


    Author Comment

    its 'crying' about the SQL DATA ADAPTER lines.
    what to I need to include?
    Here is my code to feed the grid. Tell me if it works with what you have in mind.
       myConnection = New SqlConnection("server=dumbo;uid=sa;pwd=sas;database=rfp01")
            Dim cmd As New System.Data.SqlClient.SqlCommand
            Dim dv As New DataView
            Dim drv As DataRowView
            Dim tval As String

                db.SelectCommand = New SqlCommand("select ps_par,convert(dec(10,0),rps_qty_req) as QtyReq, rps_site as Site, rps_line as Line, " & _
       "ps_op,ps_comp as ItemNo, convert(dec(10,5),ps_qty_per) as QtyCseBS , ps_scrp_pct as [%Scr], " & _
       "convert(dec(7,4),(ps_scrp_pct/100)) as Scrap, " & _
       "1-(ps_scrp_pct/100) as pct2, " & _
       "convert(dec(15,4),(ps_qty_per /  (1-(ps_scrp_pct/100)))) as QtyCsAS, " & _
       "convert(dec(15),(convert(dec(15,4),(ps_qty_per /  (1-(ps_scrp_pct/100)))) * rps_qty_req),1) as TotQtyReq " & _
       "from rps_mstr " & _
       "join ps_mstr pf on ps_par = rps_part " & _
       "where rps_site = '" + ListBox3.Text + "' and rps_line = '" + ListBox4.Text + "'" & _
       "and rps_due_date >= '" + TextBox1.Text + "' and rps_due_date <= '" + TextBox2.Text + "'" & _
       "and ((ps_start is null and ps_end is  null)or " & _
       "(ps_start is null and ps_end >= rps_due_date) or " & _
       "(ps_end is null and ps_start <= rps_due_date)) ", myConnection)

                db.Fill(dt, 0)
                DataGrid1.DataSource = dt.Tables(0).DefaultView

    LVL 28

    Accepted Solution

    Here is the basic code to fill dataset via dataadapter

     Dim fsConn As String = ".........."
       Dim ds As DataSet
       Dim da As OleDbDataAdapter
       Dim fsSQL As String = "SELECT * FROM Table bla bla"
       Dim fsSource As String = "Test"

       Private Sub CreateDS()
           ds = New DataSet()
           'Connect to database and specify sSQL
           da = New OleDbDataAdapter(fsSQL, fsConn)

               da.Fill(ds, fsSource) 'fsSource is a table
       End Sub

    after that
    DataGrid1.DataSource = ds.Tables(0)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now