Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Can anyone please help me in figuring out what change needs to be done to the below code in order to show the chart (the chart I want is displayed only when I click the "Switch Row/Column" button on the toolbar, I want it to be displayed automatically without manually clicking the button). Below is my code:

Private Sub DisplayGraph()
        Try
            Dim xlApp As Excel.Application
            Dim xlWorkBook As Excel.Workbook
            Dim xlWorkSheet As Excel.Worksheet
            Dim misValue As Object = System.Reflection.Missing.Value
 
            xlApp = New Excel.ApplicationClass
            xlWorkBook = xlApp.Workbooks.Add(misValue)
            xlWorkSheet = xlWorkBook.Sheets("sheet1")
            xlApp.Visible = True
 
            'Adding Data..
            xlWorkSheet.Cells(1, 1) = 2
            xlWorkSheet.Cells(1, 2) = 1
            xlWorkSheet.Cells(2, 1) = 3
            xlWorkSheet.Cells(2, 2) = 2
            xlWorkSheet.Cells(3, 1) = 2
            xlWorkSheet.Cells(3, 2) = 5
            xlWorkSheet.Cells(4, 1) = 4
            xlWorkSheet.Cells(4, 2) = 7
            xlWorkSheet.Cells(5, 1) = 8
            xlWorkSheet.Cells(5, 2) = 2
            xlWorkSheet.Cells(6, 1) = 5
            xlWorkSheet.Cells(6, 2) = 3
 
            Dim oChart As Excel._Chart
            oChart = xlWorkBook.Charts.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value)
 
            oChart.ChartWizard(xlWorkSheet.Range("A1", "B6"), Excel.XlChartType.xlXYScatterLines, Missing.Value, _
                                Excel.XlRowCol.xlRows, _
                                Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, _
                                Missing.Value, Missing.Value)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
 
    End Sub 
Posted
Updated 5-Jan-10 19:24pm
v2

1 solution

There should be a more direct solution that just specifies the right format, etc. straight away, but this fixes the problem as you've described it, and because we're using VB.Net, I've removed the unneeded Missing references. (I guess you converted this from a C# sample.)
Private Sub DisplayGraph()
    Try
        Dim xlApp As Excel.Application
        Dim xlWorkBook As Excel.Workbook
        Dim xlWorkSheet As Excel.Worksheet

        xlApp = New Excel.ApplicationClass
        xlWorkBook = xlApp.Workbooks.Add()
        xlWorkSheet = xlWorkBook.Sheets("sheet1")
        xlApp.Visible = True

        'Adding Data..
        xlWorkSheet.Cells(1, 1) = 2
        xlWorkSheet.Cells(1, 2) = 1
        xlWorkSheet.Cells(2, 1) = 3
        xlWorkSheet.Cells(2, 2) = 2
        xlWorkSheet.Cells(3, 1) = 2
        xlWorkSheet.Cells(3, 2) = 5
        xlWorkSheet.Cells(4, 1) = 4
        xlWorkSheet.Cells(4, 2) = 7
        xlWorkSheet.Cells(5, 1) = 8
        xlWorkSheet.Cells(5, 2) = 2
        xlWorkSheet.Cells(6, 1) = 5
        xlWorkSheet.Cells(6, 2) = 3

        Dim oChart As Excel._Chart
        oChart = xlWorkBook.Charts.Add()

        oChart.ChartWizard(xlWorkSheet.Range("A1", "B6"), _
          Excel.XlChartType.xlXYScatterLines, _
          PlotBy:=Excel.XlRowCol.xlRows)
        oChart.PlotBy = Excel.XlRowCol.xlColumns
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900