Search code examples
c#.netmschart

Multiple Series from DataSet


enter image description hereI have a relatively simple problem, I want to display 2 simple line series in a Chart from a DataSet with 2 tables in it.

Right now I simply create a second series with the same ValueMembers but they are displayed on top of one another. The DataSet is filled correctly with different values.

      dataAdapter.Fill(dataSetChart);
      chartKunden.Series.Add("Kunden");
      chartKunden.Series.Add("Table1");
      chartKunden.Series["Kunden"].ChartType = SeriesChartType.Line;
      chartKunden.Series["Table1"].ChartType = SeriesChartType.Column;
      chartKunden.Series["Table1"].XValueMember = "Woche";  
      chartKunden.Series["Table1"].YValueMembers = "Stunden";     
      chartKunden.Series["Kunden"].XValueMember = "Woche";  
      chartKunden.Series["Kunden"].YValueMembers = "Stunden";
      chartKunden.DataSource = dataSetChart;

I basically just want to know how to seperate them so the second series gets the data from the second table of the DataSet.

Updated DataBind:

chartKunden.Series["Table2"].Points.DataBind(dataSetChart.Tables[1].Rows, "Woche", "Stunden", "");

chartKunden.Series["Table1"].Points.DataBind(dataSetChart.Tables[0].Rows, "Woche", "Stunden", "");

Solution

  • There are many ways to do databinding.

    You can bind each Series to a separate data source for example like so:

    s1.XValueMember = "col11";
    s1.YValueMembers = "col12";
    s2.XValueMember = "col21";
    s2.YValueMembers = "col22";
    
    s1.Points.DataBind(t1.Rows, "col11", "col12", "");
    s2.Points.DataBind(t2.Rows, "col21", "col22", "");
    

    This assumes a two DataTables t1 and t2 with Columns "col11", "col12" and "col21", "col22".

    Note the empty string as last parameter. Here one can add a list of comma-separated custom properties to add to the binding. Example:

    s1.Points.DataBind(t1.Rows, "col11", "col12", "Tooltip=colcom1");
    

    See here for a duscussion of limitations for this!

    Also note that this binding overload needs to find x- and y-values in the same data source. Check out the overview of bindings above for even more flexible ways!!

    A simple example to bind x- and y-values to different sources could be written as:

    s2.Points.DataBindXY(t2.Rows, "col21", t1.Rows, "col12");
    

    Note that now we can't set extended properties!