Solved

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

Posted on 2011-02-22
6
557 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:Karen Wilson
  • 4
6 Comments
 
LVL 2

Expert Comment

by:jondow
ID: 34951913
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

0
 
LVL 10

Expert Comment

by:P1ST0LPETE
ID: 34951940
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))
0
 

Author Comment

by:Karen Wilson
ID: 34953678
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


0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Karen Wilson
ID: 34953980
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?

0
 

Accepted Solution

by:
Karen Wilson earned 0 total points
ID: 35232648
Dim fourRows As DataTable
        fourRows = dTable.Clone

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


        Me.chtHealth.Series.Clear()
        Me.chtHealth.DataSource = fourRows
        Me.chtHealth.DataBind()

        Try
            Dim columnCount = fourRows.Columns.Count - 1
            Me.chtHealth.Series.Clear()
            Me.chtHealth.ChartAreas("ChartArea1").IsSameFontSizeForAllAxes = True


            Dim lastColumn As Integer = fourRows.Columns.Count

            For dC As Integer = 1 To columnCount - 2

                Dim colHeader As String = fourRows.Columns(dC).ColumnName
                Me.chtHealth.Series.Add(colHeader)
                Me.chtHealth.Series(colHeader).ChartType = SeriesChartType.StackedColumn
                Me.chtHealth.Series(colHeader).BorderColor = Color.Black
                Me.chtHealth.Series(colHeader).BorderWidth = 1
                Me.chtHealth.Series(colHeader).XValueType = DataVisualization.Charting.ChartValueType.String
                Me.chtHealth.Series(colHeader).YValueType = DataVisualization.Charting.ChartValueType.Double
                Me.chtHealth.Series(colHeader).XValueMember = "Well Id"
                Me.chtHealth.Series(colHeader).YValueMembers = colHeader
                Me.chtHealth.Series(colHeader).ToolTip = "#VALX:  #VALY{N2}"
                Me.chtHealth.Series(colHeader).CustomProperties = "DrawingStyle=Cylinder"
            Next


            Dim totHeader As String = fourRows.Columns(lastColumn - 2).ColumnName
            Me.chtHealth.Series.Add(totHeader)
            Me.chtHealth.Series(totHeader).ChartType = SeriesChartType.Line
            Me.chtHealth.Series(totHeader).BorderWidth = 0
            Me.chtHealth.Series(totHeader).MarkerStyle = MarkerStyle.Diamond
            Me.chtHealth.Series(totHeader).MarkerColor = Color.Red
            Me.chtHealth.Series(totHeader).MarkerSize = 8
            Me.chtHealth.Series(totHeader).MarkerBorderColor = Color.Black

            Me.chtHealth.Series(totHeader).XValueType = DataVisualization.Charting.ChartValueType.String
            Me.chtHealth.Series(totHeader).YValueType = DataVisualization.Charting.ChartValueType.Double
            Me.chtHealth.Series(totHeader).XValueMember = "Well Id"
            Me.chtHealth.Series(totHeader).YValueMembers = totHeader
            Me.chtHealth.Series(totHeader).ToolTip = "#VALX:  #VALY{N2}"

            Dim cumHeader As String = fourRows.Columns(lastColumn - 1).ColumnName
            Me.chtHealth.Series.Add(cumHeader)
            Me.chtHealth.Series(cumHeader).ChartType = SeriesChartType.Line
            Me.chtHealth.Series(cumHeader).BorderWidth = 4
            Me.chtHealth.Series(cumHeader).MarkerStyle = MarkerStyle.Circle
            Me.chtHealth.Series(cumHeader).MarkerColor = Color.Yellow
            Me.chtHealth.Series(cumHeader).MarkerBorderColor = Color.Black
            Me.chtHealth.Series(cumHeader).XValueType = DataVisualization.Charting.ChartValueType.String
            Me.chtHealth.Series(cumHeader).YValueType = DataVisualization.Charting.ChartValueType.Double
            Me.chtHealth.Series(cumHeader).XValueMember = "Well Id"
            Me.chtHealth.Series(cumHeader).YValueMembers = cumHeader
            Me.chtHealth.Series(cumHeader).ToolTip = "#VALX:  #VALY{N2}"
            Me.chtHealth.Series(cumHeader).Label = "#VALY{N2}"
            Me.chtHealth.Series(cumHeader).LabelForeColor = Color.White


            Me.chtHealth.Titles("Title2").Text = ("Date Range Between: ") & CStr(pckrStartDate.Text) & " and " & CStr(pckrEndDate.Text)
            Me.chtHealth.Visible = True
            Me.chtGalDate.Titles("Title2").Text = ("Date Range Between: ") & CStr(pckrStartDate.Text) & " and " & CStr(pckrEndDate.Text)
            Me.chtGalDate.Visible = True

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

        End Try
0
 

Author Closing Comment

by:Karen Wilson
ID: 35304251
I figured out the import rows and the solution for my chart.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
A short film showing how OnPage and Connectwise integration works.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now