VS 2010 Problems with merging datatables-VBForums
Results 1 to 12 of 12

Thread: Problems with merging datatables

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2012
    Posts
    58

    Problems with merging datatables

    Hey,

    I'm ultimately trying to create a new series for each datatable. I am trying to do it by merging the datatables creating a new series each 2 columns in the new datatable.
    The only problem is I hit this error:

    Series data points do not support values of type System.Data.DataRowView only values of these types can be used: Double, Decimal, Single, int, long, uint, ulong, String, DateTime, short, ushort.

    every time it runs through for the second time (to add a new series).

    Here is all the relevant code (I hope)

    Code:
    Imports WindowsApplication2.ProjectileForm
    Imports System.Math
    Imports System.IO
    Imports System.Runtime.Serialization
    
    Imports System.ComponentModel
    Imports System.Runtime.Serialization.Formatters
    
    Public Class ProjectileForm
    
    
        Public calculator As New Calculate
    
        Public g As Double = 9.8
    
        Public counteruno As Integer = 1
        Public counterdos As Integer = -1
    
        Dim chartmaximumX As Integer
    
        Public count As Integer = 0
    
        Dim chartmaximumY As Double
        Dim lastmaxX As Double
        Dim lastmaxY As Double
        Dim datatable As DataTable 'new?
        Public dset As New DataSet
    
        Dim velocity As Double
        Dim angle As Double
        Dim ivelocityX As Double
        Dim ivelocityY As Double
        Dim roundedY As Double
        Dim x As Double
        Dim y As Double 'not all variables needed, investigate later
        Dim maxY As Double
        Dim maxX As Double
        Dim t As Double = 0
        Dim maxT As Double
        Dim RoundedX As Integer
    
        Public dt As New DataTable
        Public firstiteration As Boolean = True
     
        Private Sub Simulate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles simulate.Click
    
            calculator.Projectile(g, ivelocityX, ivelocityY, maxT, firstiteration, counteruno, counterdos)
    
            Dim NewTab As New TabPage
            Dim datagrids As New DataGridView
            Chart1.Series.Add(count)
        
            Chart1.Series(count).ChartType = DataVisualization.Charting.SeriesChartType.Spline
            Chart1.DataSource = dt
            Chart1.Series(count).XValueMember = (counteruno)
            Chart1.Series(count).YValueMembers = (counterdos)
          
            NewTab.Name = (count + 1)
            NewTab.Text = NewTab.Name
            datagrids.Dock = DockStyle.Fill
            datagrids.RowHeadersVisible = False
    
            Dim datatable As New DataTable
            datatable.Columns.Add("X data")
            datatable.Columns.Add("Y data")
    
    
    
    
    
            count += 1
            Me.TabControl1.Controls.Add(NewTab)
            NewTab.Controls.Add(datagrids)
            Me.TabControl1.SelectedIndex = Me.TabControl1.TabCount - 1
    
    
    
            'Dim series As DataVisualization.Charting.Series = New DataVisualization.Charting.Series()
         
            DataGridView1.DataSource = dt
            datagrids.DataSource = datatable
            Chart1.DataSource = dt
    
               Chart1.ChartAreas(0).AxisX.IntervalOffset = 1
    
            lastmaxX = Round(chartmaximumX, 0)
            lastmaxY = Round(chartmaximumY, 0)
    
            Chart1.ChartAreas(0).AxisX.IsStartedFromZero = True
            Chart1.ChartAreas(0).AxisY.IsStartedFromZero = True
    
    
            counteruno = counteruno + 1
            counterdos = counterdos - 1
           
        End Sub
    
    
    Public Class Calculate
    
        Public Sub Projectile(ByVal g As Double, ByVal ivelocityX As Double, ByVal ivelocityY As Double, ByVal maxt As Double, ByVal firstiteration As Boolean, ByRef counteruno As Integer, ByRef counterdos As Integer)
    
            Dim datatable As New DataTable
            Dim x As Double = 0
            Dim y As Double = 0
            Dim t As Double = 0 
    
      
            datatable.Columns.Add(counteruno)
            datatable.Columns.Add(counterdos)
    
            Do
                If firstiteration = True Then
                    Dim firstrow As DataRow = datatable.NewRow
                    firstrow.SetField(0, 0)
                    firstrow.SetField(1, 0)
                    datatable.Rows.Add(firstrow)
                    firstiteration = False
                Else
                End If
    
                t = t + (maxt / 100)
                x = Round(ivelocityX * t, 3)
                y = Round(ivelocityY * t + 0.5 * -g * (t ^ 2), 3)
    
                Dim nextRow As DataRow = datatable.NewRow
                Dim lastrow As DataRow = datatable.NewRow
    
                If y > 0 Then
                    nextRow.SetField(0, x)
                    nextRow.SetField(1, y)
                    datatable.Rows.Add(nextRow)
    
                Else
                    t = maxt
                    y = 0
    
                    lastrow.SetField(0, x)
                    lastrow.SetField(1, y)
                    datatable.Rows.Add(lastrow)
    
                End If
            Loop Until t >= maxt
    
    
            If firstiteration = True Then
                ProjectileForm.dt = datatable
    
            Else
                  ProjectileForm.dt.Merge(datatable)
            End If
            ProjectileForm.dt = datatable
    
    
            ProjectileForm.DataGridView1.DataSource = ProjectileForm.dt
            ProjectileForm.dset.Tables.Add(datatable)
        End Sub
    
    End Class
    Thanks : )

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    30,830

    Re: Problems with merging datatables

    First, change the name of that datatable to something other than datatable. That's just going to cause trouble. Technically, you can have a variable with the same name as a datatype, but it isn't a good idea.

    Aside from that, on which line are you getting the problem? Your description suggests that you are getting a conversion problem, but I'm not sure quite where. I do wonder about these two lines:

    datatable.Columns.Add(counteruno)
    datatable.Columns.Add(counterdos)

    since they seem likely to cause trouble, but maybe not. I've never tried creating a column without much of a name before.


    This part also doesn't look so good:

    If firstiteration = True Then
    ProjectileForm.dt = datatable

    Else
    ProjectileForm.dt.Merge(datatable)
    End If
    ProjectileForm.dt = datatable
    After all, this will do nothing if the If is true, other than set the .dt = datatable twice. However, every other time, it will merge datatable into dt (if that is even possible), then overwrite it with datatable thereby wiping out the merge.
    My usual boring signature: Nothing

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,242

    Re: Problems with merging datatables

    Might be nice to know exactly which line the error occurs on.

    I do wonder about these two lines:

    datatable.Columns.Add(counteruno)
    datatable.Columns.Add(counterdos)

    since they seem likely to cause trouble, but maybe not.
    You'd get away with it with Option Strict Off as it will infer conversion to a string, it being the only possible type for the overload. Not wise though. Because I suspect that you won't get away with it here ...

    Chart1.Series(count).XValueMember = (counteruno)
    Chart1.Series(count).YValueMembers = (counterdos)
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  4. #4

    Thread Starter
    Member
    Join Date
    Jun 2012
    Posts
    58

    Re: Problems with merging datatables

    The error doesn't happen on a specific line, it pops up with no source available and this message -
    Series data points do not support values of type System.Data.DataRowView only values of these types can be used: Double, Decimal, Single, int, long, uint, ulong, String, DateTime, short, ushort.

    Dt and datatable are in seperate classes. Dt is bound to the chart, I think that it gets merged to make datatable have all the old values + it's own. After that dt = datatable to carry over the merged results back to the main form. I think.

    Thanks

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    30,830

    Re: Problems with merging datatables

    After you add the new information to Dt, put a breakpoint on the next line and take a look at Dt. I don't see it, but it seems pretty clear what is going on: You aren't putting into Dt what you think you are putting into Dt, so if you watch what you are putting in there, you will find that it is a DataRowView rather than a value.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Member
    Join Date
    Jun 2012
    Posts
    58

    Re: Problems with merging datatables

    Sorry about the wait, I was away. You're right, it seems to replace it with the new datatable and then add a dataviewrow, which causes it to crash. Why does it try add a dataviewrow rather than the original datatable?

  7. #7

    Thread Starter
    Member
    Join Date
    Jun 2012
    Posts
    58

    Re: Problems with merging datatables

    The problem has evolved. Now, using

    Code:
      If firstiteration = True Then
                ProjectileForm.dt = datatable
    
            Else
           
                ProjectileForm.dt.Merge(datatable)
            End If
    It has no problem creating a new series and all the data is in the table, but it keeps re-using the X values from the first run through. The Y values update properly, and I can't find the fault in my code which causes it.

    Name:  graph problem example.JPG
Views: 348
Size:  19.8 KB

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    30,830

    Re: Problems with merging datatables

    So, you put a breakpoint somewhere early in that code then stepped through looking at what x value it is writing, and they looked correct when they were written? If that is the case, then something else is changing them later.
    My usual boring signature: Nothing

  9. #9

    Thread Starter
    Member
    Join Date
    Jun 2012
    Posts
    58

    Re: Problems with merging datatables

    Yeah the data in the table is fine, but for some reason it does not use the new x values...


    Name:  table.JPG
Views: 346
Size:  40.3 KB

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    30,830

    Re: Problems with merging datatables

    Well, if the datatable has the correct data in it, but the chart isn't showing it, then the problem has nothing to do with calculating information for the datatable. Instead, the problem has to do with the chart, whatever that is, and I've never used it (which should be clear, since I don't even know what it is). I would guess that you have your series set up wrong. After all, you just showed a screen shot where two different sets of data are offset by a couple rows. Since you set the series for the chart to a single value, which I assume to be a column ordinal, I don't see why it would know to pick up a different set of columns starting at a different set of rows.
    My usual boring signature: Nothing

  11. #11

    Thread Starter
    Member
    Join Date
    Jun 2012
    Posts
    58

    Re: Problems with merging datatables

    The type of chart is Spline. After each iteration the counteruno and counterdos increment and decrement respectively. The chart's xvalue member is the column with the index of whatever counteruno is, and the same goes for the Y value datamember. But for some reason it only changes the y values...?

    I appreciate your help so far, and if you fancy wading into the unknown with me, I look forward to hearing from you again

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    30,830

    Re: Problems with merging datatables

    And you put a breakpoint on this line:

    Chart1.Series(count).XValueMember = (counteruno)

    and confirmed that counteruno and counterdos are correct?

    If you have done that explicitly, then the only remaining possibility is that Chart1 is ignoring the change you are making to one of its properties. In some ways, that would make the problem easier to work with. After all, you could ignore that variable and set the YValueMember to various things (not variables, just give it various numbers) to see whether you could get the chart to change at all. If you can't get it to change when you change the YValueMember, and can get it to change when you change the XValueMember, then you have found a characteristic of the Chart itself. It may be there for a reason, so you'd want to carefully check the documentation to see whether or not it is documented. If it is not, it may be a bug. There could be a workaround for that, but it would be kind of a remarkable find. Therefore, none of this should be considered until you have stepped through the code to watch how the property changes when you assign a new value to it.
    My usual boring signature: Nothing

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.