Link to home
Start Free TrialLog in
Avatar of Karen Wilson
Karen WilsonFlag for United States of America

asked on

How do I request a specific number of rows from a data table for a chart in Visual Studio 2010.

I am trying to build a chart using a data table.  The top 4 rows of the table, I would like to make stacked columns.  The 5th row, I want to make a marker on top of the stacked column and the 6th row, I want to make a line on the chart.

All I seem to be able to do is make all the rows stacked columns!  I can't even seem to figure out how to just pluck out the first four rows.  HELP!

Here is one of the many series of code I have been trying:

Try

            Dim columnCount = dTable.Columns.Count - 1
            Dim rowCount = dTable.Rows.Count - 1

            For dC As Integer = 1 To columnCount - 1

                Dim colHeader As String = dTable.Columns(dC).ColumnName
                Me.chtHealth.Series.Add(colHeader)
                Me.chtHealth.Series(colHeader).XValueType = DataVisualization.Charting.ChartValueType.String
                Me.chtHealth.Series(colHeader).YValueType = DataVisualization.Charting.ChartValueType.Double

                For dR As Integer = 0 To rowCount - 2
                    Me.chtHealth.Series(colHeader).XValueMember = "Well ID" ' I just want the first freaking 4 rows on the chart!!!!!!!  
                    Me.chtHealth.Series(colHeader).YValueMembers = colHeader
                    Me.chtHealth.Series(colHeader).ToolTip = "#VALX:  #VALY{N2}"
                    Me.chtHealth.Series(colHeader).CustomProperties = "DrawingStyle=Emboss"
                    Me.chtHealth.Series(colHeader).ChartType = SeriesChartType.StackedColumn
                Next dR

            Next dC

            Me.chtHealth.Visible = True

        Catch ex As Exception
            MessageBox.Show(ex.Message, ex.GetType.ToString)

        End Try

Good Karma to the person that can solve this mystery!!
Thanks.
Avatar of jondow
jondow
Flag of United Kingdom of Great Britain and Northern Ireland image

The following will get you the first 4 rows:

Regards

Rick
Dim dt As New System.Data.DataTable

        Dim dr As System.Data.DataRow

        For i As Integer = 0 To 3
            dr = dt.Rows(i)
            'Get the item from the datarow where "1" is the column index
            Dim x As String = dr.Item(1).ToString
        Next

Open in new window

Separating the data:

        Dim table As New DataTable
        'Populate the data table here...

        Dim fourRows As DataTable
        fourRows = table.Clone() 'Copies the table schema of the source

        'Separate the first 4 rows into a separate table:
        For i As Integer = 0 To 3
            fourRows.Rows.Add(table.Rows(i))
        Next i

        Dim markerRow As DataTable
        markerRow = table.Clone
        markerRow.Rows.Add(table.Rows(4))

        Dim lineRow As DataTable
        lineRow = table.Clone
        lineRow.Rows.Add(table.Rows(5))
Avatar of Karen Wilson

ASKER

I tried both ways and a variation of both and it won't get past the section of

 For i As Integer = 0 To 3
            fourRows.Rows.Add(table.Rows(i))
        Next i

ERROR MESSAGE:  This row already belongs to another table

or
For i As Integer = 0 To 3
            dr = dt.Rows(i)
            'Get the item from the datarow where "1" is the column index
            Dim x As String = dr.Item(1).ToString
        Next
ERROR MESSAGE:  There is no row at position 0


Dim fourRows As New DataTable
            fourRows = dTable.Clone

            For i As Integer = 0 To 3
                fourRows.ImportRow(dTable.Rows(i))
            Next i

I searched around the internet and found this and it works!!  It's "importRow"

Now that I have segregated the data, do either of you know how to use segregated data tables for a chart?

ASKER CERTIFIED SOLUTION
Avatar of Karen Wilson
Karen Wilson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I figured out the import rows and the solution for my chart.