datagrid binding problem

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

    datagrid binding problem

    What I have is three forms. Form one with buttons 1, 2 and 3; and forms 2 and 3 with datagrids.

    Button 1 populates arrays a(i,j) and b(i,j) with values.

    Button 2 creates a datatable containing the a(i,j) array then adds that table to a dataset
    which is in turn bound to a datagrid in an instance of form 2 called matrixa.

    Button 3 creates a datatable containing the b(i,j) array then adds that table to a dataset
    which is in turn bound to a datagrid in an instance of form 3 called matrixb.

    When I start the debugger and click in sequence buttons 1 2 and 3, everything goes exactly as planned. If,
    however I then press button 2 again I get an error: "Column 'Column1" does not belong to matrixa"

    I have discovered that the problem dissapears if I have button 2 call populatea() twice.

    What's going on here?
    :
    #Region "Public dimension statements"

    Public a_m As Integer = 5
    Public a_n As Integer = 5
    Public a(6, 6) As Double
    Dim matrix_A As New form2

    Public b_m As Integer = 5
    Public b_n As Integer = 5
    Public b(6, 6) As Double
    Dim matrix_B As New Form3
    #End Region


    #Region "populate arrays a and b"

    Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click
    Dim i As Integer
    Dim j As Integer
    For i = 1 To 5
    For j = 1 To 5
    a(i, j) = i + j
    b(i, j) = i * j
    Next
    Next
    End Sub
    #End Region


    Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button2.Click
    populateA()

    End Sub
    Private Sub Button3_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button3.Click
    populateB()


    End Sub
    #Region "This is the 'A' data grid populate section"

    Private Sub populateA()
    Dim dta As New DataTable("matr ixa")
    Dim dca(a_n + 1) As DataColumn
    Dim dra(a_m + 1) As DataRow
    Dim dataset1 As New DataSet

    'loop in columns
    Dim i As Integer = 0
    Dim j As Integer = 0

    For j = 0 To a_n - 1
    dca(j) = New DataColumn("")
    dca(j).DataType = System.Type.Get Type("System.Si ngle")
    dta.Columns.Add (dca(j))
    Next

    'create rows
    For i = 0 To a_m - 1
    dra(i) = dta.NewRow
    For j = 0 To a_n - 1
    dra(i)(j) = a(i + 1, j + 1)
    Next j
    Next i

    'add rows to table
    For i = 0 To a_m - 1
    dta.Rows.Add(dr a(i))
    Next

    'create instance of dataset
    dataset1 = New DataSet

    'use add method to enter table into dataset
    dataset1.Tables .Add(dta)

    'bind the dataset to the grid
    matrix_A.Text = "MATRIX A"
    matrix_A.MdiPar ent = Me
    matrix_A.DataGr id1.SetDataBind ing(dataset1, "matrixa")
    matrix_A.Show()
    dta = Nothing
    dataset1 = Nothing
    System.GC.Colle ct()
    End Sub
    #End Region
    #Region "This is the 'B' data grid populate section"

    Private Sub populateB()
    Dim dtb As New DataTable("matr ixb")
    Dim dcb(b_n + 1) As DataColumn
    Dim drb(b_m + 1) As DataRow
    Dim dataset2 As New DataSet

    'loop in columns
    Dim i As Integer
    Dim j As Integer

    For j = 0 To b_n - 1
    dcb(j) = New DataColumn
    dcb(j).DataType = System.Type.Get Type("System.Si ngle")
    dtb.Columns.Add (dcb(j))
    Next

    'create rows
    For i = 0 To b_m - 1
    drb(i) = dtb.NewRow
    For j = 0 To b_n - 1
    drb(i)(j) = b(i + 1, j + 1)
    Next j
    Next i

    'add rows to table
    For i = 0 To b_m - 1
    dtb.Rows.Add(dr b(i))
    Next

    'create instance of dataset
    dataset2 = New DataSet

    'use add method to enter table into dataset
    dataset2.Tables .Add(dtb)

    'bind the dataset to the grid
    matrix_B.Text = "MATRIX B"
    matrix_B.MdiPar ent = Me
    matrix_B.DataGr id2.SetDataBind ing(dataset2, "matrixb")
    matrix_B.Show()
    dtb = Nothing
    dataset2 = Nothing
    System.GC.Colle ct()



    End Sub
    #End Region

    --
    mark
  • Cor Ligthert

    #2
    Re: datagrid binding problem

    Mark,

    I do not see the reason, however when you send a problem to the newsgroup
    than it has to be I think for this problem like this, however make it
    readable for us.

    When you say you do something with button 2 set than the procedure in
    button2 and not in a Sub.

    As well tell in what line the problem occurs, I assume in this one however
    when that stays a question helping is much more difficult.
    [color=blue]
    > dta.Columns.Add (dca(j))[/color]

    (Do you want to keep that array structure by the way, or are you using the
    logic from the dataset, when you take the last as would be normal I would
    not create those datacolumns/rows in a matrix before).

    You are now creating ttree reference systems (datagrid, datatable, array of
    items).

    Cor


    Comment

    • Cor Ligthert

      #3
      Re: datagrid binding problem

      Mark,

      I do not see the reason, however when you send a problem to the newsgroup
      than it has to be I think for this problem like this, however make it
      readable for us.

      When you say you do something with button 2 set than the procedure in
      button2 and not in a Sub.

      As well tell in what line the problem occurs, I assume in this one however
      when that stays a question helping is much more difficult.
      [color=blue]
      > dta.Columns.Add (dca(j))[/color]

      (Do you want to keep that array structure by the way, or are you using the
      logic from the dataset, when you take the last as would be normal I would
      not create those datacolumns/rows in a matrix before).

      You are now creating ttree reference systems (datagrid, datatable, array of
      items).

      Cor


      Comment

      • mark

        #4
        Re: datagrid binding problem

        The matrix is funamental. The datagrid is for show. The datatable is a necessary go between. the line number is not identified.

        You understand language barriers and I hope you can be more flexable with code presentation

        You responce was appreciated.
        mark


        "Cor Ligthert" wrote:
        [color=blue]
        > Mark,
        >
        > I do not see the reason, however when you send a problem to the newsgroup
        > than it has to be I think for this problem like this, however make it
        > readable for us.
        >
        > When you say you do something with button 2 set than the procedure in
        > button2 and not in a Sub.
        >
        > As well tell in what line the problem occurs, I assume in this one however
        > when that stays a question helping is much more difficult.
        >[color=green]
        > > dta.Columns.Add (dca(j))[/color]
        >
        > (Do you want to keep that array structure by the way, or are you using the
        > logic from the dataset, when you take the last as would be normal I would
        > not create those datacolumns/rows in a matrix before).
        >
        > You are now creating ttree reference systems (datagrid, datatable, array of
        > items).
        >
        > Cor
        >
        >
        >[/color]

        Comment

        • Cor Ligthert

          #5
          Re: datagrid binding problem

          Mark,

          Where did the error occurs, you did not write that?

          Cor


          Comment

          • mark

            #6
            Re: datagrid binding problem


            --
            mark


            "Cor Ligthert" wrote:
            [color=blue]
            > Mark,
            >
            > Where did the error occurs, you did not write that?
            >
            > Cor
            >
            >
            > I am not sure I understand your question.[/color]

            When I start the debugger and click in sequence buttons 1 2 and 3, everything goes exactly as planned. If, however I then press button 2 again I get an error: "Column 'Column1" does not belong to matrixa"

            I believe this error I am seeing is a bug in VB.net - a serious one. It seems tedious I know, but could you try reporoducing the error by pasting my code into your developer. Use the separate populatea() function version I provided. This way you will be able to see how calling the function twice in a row eliminates the effect.






            Comment

            • Cor Ligthert

              #7
              Re: datagrid binding problem

              Mark,

              I assume that this is what you want to do
              matrix_A.MdiPar ent = Me
              matrix_A.DataGr id1.DataSource = Nothing
              matrix_A.DataGr id1.DataSource = dataset1.Tables ("matrixa")
              matrix_A.Show()

              And delete than those 3 instructions after that.
              When you would want to clear the dataset that is
              dataset.clear
              calling the GC cost only extra processing time.

              The Datagrid holds no data only references to it in the datasource, so when
              you clear the dataset that one is empty.

              (That databinding from the datagrid is the same by the way, however this is
              more done).

              I hope this helps?

              Cor


              Comment

              • mark

                #8
                Re: datagrid binding problem

                Cor;

                Yes your techn ique works well.

                Is it possible to bind the datagrid directly to the array - bypassing the datatable altogether?




                --
                mark


                "Cor Ligthert" wrote:
                [color=blue]
                > Mark,
                >
                > I assume that this is what you want to do
                > matrix_A.MdiPar ent = Me
                > matrix_A.DataGr id1.DataSource = Nothing
                > matrix_A.DataGr id1.DataSource = dataset1.Tables ("matrixa")
                > matrix_A.Show()
                >
                > And delete than those 3 instructions after that.
                > When you would want to clear the dataset that is
                > dataset.clear
                > calling the GC cost only extra processing time.
                >
                > The Datagrid holds no data only references to it in the datasource, so when
                > you clear the dataset that one is empty.
                >
                > (That databinding from the datagrid is the same by the way, however this is
                > more done).
                >
                > I hope this helps?
                >
                > Cor
                >
                >
                >[/color]

                Comment

                • Cor Ligthert

                  #9
                  Re: datagrid binding problem

                  Mark,

                  When I have understand this documentation well, it should go, however I
                  never tried that really because of what I write after the links.





                  Why not use the datatable as your datasource it is much more easier to
                  reference to than an array

                  dt.rows(0)(0) is the first item

                  dt.(rows(dt.row s.count-1)(dt.rows.item list.count-1) is the last one.

                  There are a lot of features in a datatable you do not have in an array.

                  (And before I forget I think it is better to set allowsort to false in your
                  datagrid).

                  I hope this helps?

                  Cor
                  [color=blue]
                  > Cor;
                  >
                  > Yes your techn ique works well.
                  >
                  > Is it possible to bind the datagrid directly to the array - bypassing the[/color]
                  datatable altogether?[color=blue]
                  >
                  >
                  >
                  >
                  > --
                  > mark
                  >
                  >
                  > "Cor Ligthert" wrote:
                  >[color=green]
                  > > Mark,
                  > >
                  > > I assume that this is what you want to do
                  > > matrix_A.MdiPar ent = Me
                  > > matrix_A.DataGr id1.DataSource = Nothing
                  > > matrix_A.DataGr id1.DataSource = dataset1.Tables ("matrixa")
                  > > matrix_A.Show()
                  > >
                  > > And delete than those 3 instructions after that.
                  > > When you would want to clear the dataset that is
                  > > dataset.clear
                  > > calling the GC cost only extra processing time.
                  > >
                  > > The Datagrid holds no data only references to it in the datasource, so[/color][/color]
                  when[color=blue][color=green]
                  > > you clear the dataset that one is empty.
                  > >
                  > > (That databinding from the datagrid is the same by the way, however this[/color][/color]
                  is[color=blue][color=green]
                  > > more done).
                  > >
                  > > I hope this helps?
                  > >
                  > > Cor
                  > >
                  > >
                  > >[/color][/color]


                  Comment

                  Working...