VS 2010 Charts (Not Excel) and referencing the datasource when it's table is out of scope-VBForums
Results 1 to 7 of 7

Thread: Charts (Not Excel) and referencing the datasource when it's table is out of scope

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2012
    Location
    Wiltshire, England
    Posts
    211

    Question Charts (Not Excel) and referencing the datasource when it's table is out of scope

    Hi,

    I have a chart whose datasource is set to a locally defined datatable, but initially I only want to display some of the data columns as dataseries as there are too many. Later the user will then pick which extra dataseries they want to see - I currently do this by storing the locally defined datatable in a public dataset which I use to store tables that I might need later (There are no databases involved). This works OK, but I was wondering that as the chart knows the name of the datatable it is using as it datasource that I might be able to add / remove dataseries without having to go off an retrieve a reference to the datatable again.

    My Code for setting the chart up initially (The extra dataseries all use the 2nd Y Axis)

    Code:
                        With chartSingle_Data_File
                            .DataSource = dtSelected_Data_File_and_PMT
                            .ChartAreas(0).AxisX.Title = "WL"
                            .ChartAreas(0).AxisY.Title = "CD"
                            .ChartAreas(0).AxisY2.Enabled = AxisEnabled.True
                            .ChartAreas(0).AxisY2.Title = "Volts"
    
                            Do While .Series.Count > 0
                                .Series.RemoveAt(0)
                            Loop
    
                            For intSeries_Collection_Count = 0 To intSeries_Collection_Total - 1 
                                .Series.Add(dtSelected_Data_File_and_PMT.Columns(intSeries_Collection_Count + 1).ColumnName)
                                .Series(intSeries_Collection_Count).XValueMember = "WL"
                                .Series(intSeries_Collection_Count).YValueMembers = dtSelectedFile.Columns(intSeries_Collection_Count + 1).ColumnName '
                                .Series(intSeries_Collection_Count).ChartType = SeriesChartType.Line
                                .Series(intSeries_Collection_Count).YAxisType = AxisType.Primary
                            Next intSeries_Collection_Count
    
    End With
    I was hoping to be able to do something like this:

    Code:
    chartSingle_Data_File.datasource.Series.Add.Columns(intSeries_Collection_Count + 1)
    Thanks

    Kristian
    Last edited by kriswork; Sep 28th, 2015 at 08:25 AM. Reason: increase clarity

  2. #2
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,204

    Re: Charts (Not Excel) and referencing the datasource when it's table is out of scope

    so your datatable has all the columns already and is not changing during the process, right? i think to add another series to the chart you would Need to do the stuff that is inside your "For intSeries_Collection_Count = 0 To intSeries_Collection_Total - 1" Loop for the new column. no?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2012
    Location
    Wiltshire, England
    Posts
    211

    Re: Charts (Not Excel) and referencing the datasource when it's table is out of scope

    Quote Originally Posted by digitalShaman View Post
    so your datatable has all the columns already and is not changing during the process, right? i think to add another series to the chart you would Need to do the stuff that is inside your "For intSeries_Collection_Count = 0 To intSeries_Collection_Total - 1" Loop for the new column. no?
    All my code above runs say when the form loads and you are correct that dtSelected_Data_File_and_PMT won't change, but initially I only want to display half the columns as data series. Possibly later a user will click a radio button which will then add the other half of columns as data series. The problem is that dtSelected_Data_File_and_PMT will be out of scope by then as it was defined on the form load event and so how do I add a new series to my chart which already exists in the datasource?- What I do at the moment is define a new table and point at the table stashed in the dataset - I was just hoping that the chart would keep its own copy of the table that was its datasource and allow my to add new dataseries from it existing datasource. It would be great if you could do the same with DGVs as well where if you clicked on a cell you could then use the DGV's datasource property to access additional information from its table that you may not to display all the time. What complicates matters is that most of my controls are created on the fly and there could be many copies of the same control just with different datasources.

  4. #4
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,204

    Re: Charts (Not Excel) and referencing the datasource when it's table is out of scope

    the Chart does not hold a copy of the table but the table is still in your dataset so you can always recreate a reference to it using dtSelected_Data_File_and_PMT=yourdataset("yourtablename")
    maybe i am misunderstanding your Problem. it might help if you provie a stipped out Version of the code so that we can see the form_load Event where you query and store the data in the dataset and set up the Chart for the first time.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2012
    Location
    Wiltshire, England
    Posts
    211

    Re: Charts (Not Excel) and referencing the datasource when it's table is out of scope

    The dataset solution is OK and means I don't need lots of publicly declared tables, it just means I need to be particularly careful naming my tables so I can then retrieve the right one later, whereas If I've just clicked on a chart to display more dataseries I would love to be able to do something like sender.datasource.column(intcolumn_number).add_as_series.

    Thanks for you time.

    Kristian

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jul 2012
    Location
    Wiltshire, England
    Posts
    211

    Re: Charts (Not Excel) and referencing the datasource when it's table is out of scope

    OMG - The chart does hold a copy of the table!!!

    Just commented out my function to load the table from my dataset and now directcast the table from the chart's datasource:

    Code:
        'dtBuffer_and_Scans = funcGet_Table(dsDataSet, "dtBuffer_and_Scans")
       dtBuffer_and_Scans = DirectCast(ChartBHL_Titrations.DataSource, DataTable)
    Looks like my wish came true - I bet that will work for anything that has a datasource.

    Kristian
    Last edited by kriswork; Sep 30th, 2015 at 04:26 AM. Reason: more clarity

  7. #7
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,204

    Re: Charts (Not Excel) and referencing the datasource when it's table is out of scope

    its not a copy, its a reference to the same datatable object. at least it should be.

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.