0

Hi, how to plot a chart from imported excel in Datagridview?

I tried the tutorial from HERE, but I do not know how to plot the chart.

Previously, I used MS.Access for my Datagridview, then I manage to choose the Datasource. But, for this case, I imported an excel and I cannot figured out my Datasource.

Here the code I tried for my chart:
But, got nothing

    Private Sub Chart1_Click(sender As Object, e As EventArgs) Handles Chart1.Click


        Chart1.Series("Series1").XValueMember = excelDataGridView.Rows(x).Cells(1).Value

        Chart1.Series("Series1").YValueMembers = excelDataGridView.Rows(x).Cells(4).Value

    End Sub

Full code:

Imports System.Data.OleDb
Partial Public Class Form1

    Inherits Form
    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub openFileButton_Click(sender As Object, e As EventArgs) Handles openFileButton.Click
        Dim openDialog As New OpenFileDialog()
        openDialog.Filter = "Excel|*.xlsx"
        If openDialog.ShowDialog() = DialogResult.OK Then
            excelFileTextBox.Text = openDialog.FileName
            'Get all worksheet names from the Excel file selected using GetSchema of an OleDbConnection
            Dim sourceConnectionString As String = [String].Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0 Xml;HDR=YES'", excelFileTextBox.Text)
            Dim connection As New OleDbConnection(sourceConnectionString)
            connection.Open()
            Dim tables As DataTable = connection.GetSchema("Tables", New [String]() {Nothing, Nothing, Nothing, "TABLE"})
            connection.Dispose()
            'Add each table name to the combo box
            If tables IsNot Nothing AndAlso tables.Rows.Count > 0 Then
                worksheetsComboBox.Items.Clear()
                For Each row As DataRow In tables.Rows
                    worksheetsComboBox.Items.Add(row("TABLE_NAME").ToString())
                Next
            End If
        End If
    End Sub


    Private Sub closeButton_Click_1(sender As Object, e As EventArgs) Handles closeButton.Click
        Application.[Exit]()
    End Sub


    Private Sub worksheetsComboBox_SelectedIndexChanged_1(sender As Object, e As EventArgs) Handles worksheetsComboBox.SelectedIndexChanged
        'Display the data from the selected Worksheet
        Dim sourceConnectionString As String = [String].Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0 Xml;HDR=YES'", excelFileTextBox.Text)
        Dim adapter As New OleDbDataAdapter([String].Format("SELECT * FROM [{0}]", worksheetsComboBox.SelectedItem.ToString()), sourceConnectionString)
        Dim currentSheet As New DataTable()
        adapter.Fill(currentSheet)
        adapter.Dispose()
        excelDataGridView.DataSource = currentSheet
    End Sub

    Private Sub Chart1_Click(sender As Object, e As EventArgs) Handles Chart1.Click


        Chart1.Series("Series1").XValueMember = excelDataGridView.Rows(x).Cells(1).Value

        Chart1.Series("Series1").YValueMembers = excelDataGridView.Rows(x).Cells(4).Value

    End Sub
End Class

Thanks !

1
Contributor
1
Reply
24
Views
2 Years
Discussion Span
Last Post by naz1234
0

I try to make code like this, I got error Value of type 'System.Windows.Forms.DataGridViewColumn' cannot be converted to 'String'.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Chart1.Series("Series1").XValueMember = excelDataGridView.Columns("masa")
        Chart1.Series("Series1").YValueMembers = excelDataGridView.Columns("A+B")

    End Sub
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.