• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 222
  • Last Modified:

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

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?
0
yyuubum2
Asked:
yyuubum2
  • 2
1 Solution
 
iboutchkineCommented:
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.Columns.Add(dc1)
        Next

        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,
DataTable).Rows(Me.DataGrid1.CurrentRowIndex)
            Dim drTarget As DataRow = CType(Me.DataGrid2.DataSource,
DataTable).NewRow()
            Dim i As Integer
            For i = 0 To dr.ItemArray.GetLength(0) - 1
                drTarget(i) = dr(i)
            Next
            CType(Me.DataGrid2.DataSource, DataTable).Rows.Add(drTarget)
        End If
    End Sub

0
 
yyuubum2Author Commented:
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")
        DataGrid1.Refresh()
        Dim cmd As New System.Data.SqlClient.SqlCommand
        Dim dv As New DataView
        Dim drv As DataRowView
        Dim tval As String
        Application.DoEvents()
        Try
            Application.DoEvents()
            dt.Clear()
            myConnection.Open()

            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)
            dt.AcceptChanges()
            DataGrid1.DataSource = dt.Tables(0).DefaultView


----------------------------------------------------------------------------------------------
0
 
iboutchkineCommented:
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)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now