Datagrid Datatable Insert rows and data

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rob

    Datagrid Datatable Insert rows and data

    I am trying to copy some data from one datagrid to another. The first
    datagrid containing data is called DocList. The blank Datagrid that I am
    trying to copy some data to is called DataGrid1.

    Please tell me the simplist way to loop through an existing datagrid, access
    it's columns and rows, then copy parts of that data to a blank Datagrid1.

    Thank you.

    What I've tried unsuccessfully:
    ....
    Dim dt As DataTable
    Dim dr As DataRow
    DataGrid1.DataS ource = dt

    Dim DocListGridItem As DataGridItem
    For Each DocListGridItem In DocList.Items
    dr = dt.NewRow
    dt.Rows.Add(dr)
    dr.Item(0) = DocListGridItem .Cells(0).Text( )
    Next
    ....
    --
    Rob
  • W.G. Ryan - MVP

    #2
    Re: Datagrid Datatable Insert rows and data

    Just create a second dataset 9(which I believe you already have) and then
    call Merge on the dataset passing in the first as a parameter. But I would
    ask, do you definitely need two different tables - if not, I'd be careful
    their b/c of performance issues.
    "Rob" <Rob@discussion s.microsoft.com > wrote in message
    news:4D19D8B0-A348-4619-9D69-3F558D12B191@mi crosoft.com...[color=blue]
    >I am trying to copy some data from one datagrid to another. The first
    > datagrid containing data is called DocList. The blank Datagrid that I am
    > trying to copy some data to is called DataGrid1.
    >
    > Please tell me the simplist way to loop through an existing datagrid,
    > access
    > it's columns and rows, then copy parts of that data to a blank Datagrid1.
    >
    > Thank you.
    >
    > What I've tried unsuccessfully:
    > ...
    > Dim dt As DataTable
    > Dim dr As DataRow
    > DataGrid1.DataS ource = dt
    >
    > Dim DocListGridItem As DataGridItem
    > For Each DocListGridItem In DocList.Items
    > dr = dt.NewRow
    > dt.Rows.Add(dr)
    > dr.Item(0) = DocListGridItem .Cells(0).Text( )
    > Next
    > ...
    > --
    > Rob[/color]


    Comment

    • Cor Ligthert [MVP]

      #3
      Re: Datagrid Datatable Insert rows and data

      Rob,

      Because of the fact that the datagrid does not contain any data, can you set
      the datasources of both datagrids to the same datatable.

      If it has to be datafrom the same table however filtered than you can do

      datagrid1.datas ource = dt.defaultview
      dim dv2 as new dataview(dt)
      dv.rowfilter = whatever
      datagrid1.datas ource = dv2

      If you want to show different columnst, than you use datagridcolumns tyles.

      I hope this helps,

      Cor


      Comment

      • Rob

        #4
        Re: Datagrid Datatable Insert rows and data

        What I am trying to do is iterate through the first datagrid, which contains
        essentially one column like a CSV string. Break it appart, one by one,
        massage the data, then insert it into the second initially blank datagrid in
        multiple columns row for row.

        Do you happen to have a good example of this?

        Thank you,

        Rob

        "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> wrote in message
        news:OVnrpxs7FH A.252@TK2MSFTNG P15.phx.gbl...[color=blue]
        > Rob,
        >
        > Because of the fact that the datagrid does not contain any data, can you[/color]
        set[color=blue]
        > the datasources of both datagrids to the same datatable.
        >
        > If it has to be datafrom the same table however filtered than you can do
        >
        > datagrid1.datas ource = dt.defaultview
        > dim dv2 as new dataview(dt)
        > dv.rowfilter = whatever
        > datagrid1.datas ource = dv2
        >
        > If you want to show different columnst, than you use datagridcolumns tyles.
        >
        > I hope this helps,
        >
        > Cor
        >
        >[/color]


        Comment

        • Cor Ligthert [MVP]

          #5
          Re: Datagrid Datatable Insert rows and data

          Rob,

          In my opinion is than to set an extra boolean column in your datatable

          You can than use this column in the second dataview as a filter that shows
          than the selected results in another datagrid/datagridview.

          I made for you a sample on our website, can you try that?



          I hope this helps,

          Cor


          Comment

          • Rob

            #6
            Re: Datagrid Datatable Insert rows and data

            Cor,

            Thank you for the suggestion. Unfortunately I cannot convert to Visual
            Studio 2005 and Ver 2.0 at this time. I am currently stuck in VS2003 ver
            1.1, so I'm not aware how to make the Dataview help me at this time.

            What I have tried unsuccessfully so far is with an error:

            System.NullRefe renceException: Object reference not set to an instance of an
            object
            Line 104: Dim aNewRow As DataRow =
            DsMain.Tables(" ToImport").NewR ow

            'DocList is a Datagrid containing one column of Data seperated by a period.

            Dim DsMain As New DataSet
            DsMain.Tables.A dd("ToImport")
            DsMain.Tables(" ToImport").Colu mns.Add("Col1")
            DsMain.Tables(" ToImport").Colu mns.Add("Col2")
            DsMain.Tables(" ToImport").Colu mns.Add("Col3")
            DsMain.Tables(" ToImport").Colu mns.Add("Col4")
            DsMain.Tables(" ToImport").Colu mns.Add("Col5")
            DsMain.Tables(" ToImport").Colu mns.Add("Col6")
            DsMain.Tables(" ToImport").Colu mns.Add("Col7")
            DsMain.Tables(" ToImport").Colu mns.Add("Col8")
            DsMain.Tables(" ToImport").Colu mns.Add("Col9")
            DsMain.Tables(" ToImport").Colu mns.Add("Col10" )
            DsMain.Tables(" ToImport").Colu mns.Add("Col11" )
            Dim dt As DataTable
            Dim dr As DataRow
            Dim txt As String
            Dim token As String
            Dim DocListGridItem As DataGridItem
            Dim tempint As Integer
            For Each DocListGridItem In DocList.Items
            Dim aNewRow As DataRow = DsMain.Tables(" ToImport").NewR ow
            tempint = 0
            aNewRow.Item(te mpint) =
            GetToken(DocLis tGridItem.Cells (0).Text(), ".") & vbCrLf
            Do
            If tempint = 10 Then
            tempint = 0
            DsMain.Tables(" ToImport").Rows .Add(aNewRow)
            Else
            tempint = tempint + 1
            End If
            token = GetToken("", ".")
            If token = "" Then Exit Do
            txt = txt & token & vbCrLf
            ' create new row in the Main DataSet
            aNewRow.Item(te mpint) = token
            Loop
            Next

            ' assigning the dataset object into the datagrid to accept the csv
            data broken down into multiple columns
            DataGrid1.DataS ource = DsMain
            DataGrid1.DataB ind()


            "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> wrote in message
            news:u7WnBqz7FH A.2152@TK2MSFTN GP10.phx.gbl...[color=blue]
            > Rob,
            >
            > In my opinion is than to set an extra boolean column in your datatable
            >
            > You can than use this column in the second dataview as a filter that shows
            > than the selected results in another datagrid/datagridview.
            >
            > I made for you a sample on our website, can you try that?
            >
            >[/color]
            http://www.vb-tips.com/default.aspx?...1-ac8b53818af6[color=blue]
            >
            > I hope this helps,
            >
            > Cor
            >
            >[/color]


            Comment

            • Cor Ligthert [MVP]

              #7
              Re: Datagrid Datatable Insert rows and data

              Rob,

              The sample should work for a datagrid as well, you would only have to use
              styles to get the second grid nice.

              I tested it now and saw I had to set that extra loop to set the boolean to
              false, what was not needed in that datagridview sample, however basicly it
              stays the same, however, in my opinion works a little bit nicer with the
              datagridview, because that is pushed down direct with that commit. But that
              you cannot reach in anyway with a datagrid.

              \\\
              Private Sub Form1_Load(ByVa l sender As System.Object, _
              ByVal e As System.EventArg s) Handles MyBase.Load
              'Make the basic table
              Dim dt As New DataTable
              dt.Columns.Add( "Name")
              dt.LoadDataRow( New Object() {"Ken"}, True)
              dt.LoadDataRow( New Object() {"Cor"}, True)
              dt.LoadDataRow( New Object() {"Peter"}, True)
              DataGrid1.DataS ource = dt.DefaultView
              'Adding the extra column
              dt.Columns.Add( "Check", GetType(System. Boolean))
              For Each dr As DataRow In dt.Rows
              dr("check") = False
              Next
              'Make the selection
              Dim dv As New DataView(dt)
              dv.RowFilter = "Check = true"
              DataGrid2.DataS ource = dv
              End Sub

              Private Sub DataGridView1_P aint(ByVal sender As Object, _
              ByVal e As System.Windows. Forms.PaintEven tArgs) Handles DataGrid1.Paint
              'Push the changes down to the datasource
              BindingContext( DirectCast(Data Grid1.DataSourc e,
              DataView)).EndC urrentEdit()
              End Sub
              ///

              I hope this helps,

              Cor


              Comment

              Working...