Multiple Series from DataSet - c#

I 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", "");

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!

Okay I think I could narrow down the problem, I draw the Chart in 2 different ways depending on a CheckButton, usually I draw the other version first, but now I draw the version with the mistake first and the data is now displayed correctly.
I thought I cleared the Chart and DataSet properly before adding the new Values but doesn't seem to be the case, I will further look into this.

Related

Migradoc - Tables Side by Side - not using Tables within Tables

I am using Migradoc and wish to replicate a table structure as follows:
What I really DON'T want to do is add each table into a cell of a larger table with 2 columns and 2 rows... Mainly because i) the number of tables generated can vary so it needs to be more dynamic and ii) Adding tables within tables can cause issues when spilling across to the next page.
What I had initially hoped is that I could just keep adding each new table to a TextFrame and the tables will be added horizontally and wrap to the next page if the next table wouldn't fit... Of course, not that straight forward. So far the closest I have come to even seeing a Table next to another one is with the following:
TextFrame newTF = new TextFrame
{
Width = Unit.FromPoint(200)
};
WrapFormat wf = new WrapFormat();
wf.Style = WrapStyle.Through;
newTF.WrapFormat = wf;
newTF.Add(newTable.Clone());
this.document.LastSection.Add(newTF);
TextFrame newTF2 = new TextFrame
{
Width = Unit.FromPoint(200)
};
WrapFormat wf2 = new WrapFormat();
wf2.Style = WrapStyle.Through;
newTF2.RelativeHorizontal = RelativeHorizontal.Page;
newTF2.RelativeVertical = RelativeVertical.Paragraph;
newTF2.Left = ShapePosition.Right;
newTF2.WrapFormat = wf2;
newTF2.Add(newTable.Clone());
this.document.LastSection.Add(newTF2);
However this is very static and would require a new definition for each TextFrame. Plus, the layout doesn't look amazing as the second table ignores any margins set out in the page.
If what I am asking is at all possible, am I even on the correct path? I have been looking into this all morning and am starting to feel my options sllipping away! I would have thought this would be a big requirement for a lot of Migradoc users so am surprised to find it is tricky to do? Unless I am just missing something silly of course.
Another option: create a table with 5 columns and set table borders to make it look like 2 tables with 2 columns each.
Depending on the data you add to the tables it might be better to use a table row for each line of text or have all text lines in a single row.
From your picture I assume that using a table with 2 columns and 1 row, each cell containing a table, would work, too, provided the visible tables are small enough to fit a single page.

Pie Chart Display member

I'm using a pie chart in visual studio 2010 but I don't know how to set the values on display (the values that showed on each slice).
I need to use a different value member that the X and Y values.
In a Pie Chart you only need to use the Y-Values.
So if you have a DataSource data with a Y-Value called "value" you can use this to bind:
yourSeries.Points.DataBindY(data, "value");
You can also use this, more general binding method:
yourSeries.Points.DataBind(data, "", "value", "");
Note how the first, i.e. the X-Value is left empty; all points are always added indexed anyway. The last string otherfields would be for extended properties.
And there is even another way to bind the Points of a Series:
yourSeries.Points.DataBindXY(data, "somePropery", data, "value");
Note that here you can use two different data sources. But you can't leave the name string of the first one empty, even though the actual values will be ignored againm as with any Pie chart..
Besides binding the the Points you could also use other binding methods, see here for a discussion or the pros and cons..!
Also note that the first DataPoint, i.e. the first slice starts at 0°. If you want to start it at the top set a 'Custom attribute' : PieStartAngle :
yourSeries["PieStartAngle"] = "270";
To display the values you can set this:
yourSeries.IsValueShownAsLabel = true;
To move the labels off the slices use this:
yourSeries["PieLabelStyle"] = "Outside";

Assigning a different ViewSource in WPF

I'm experimenting with C#/.net/WPF all for the first time. I've created a project and set up a datasource (just a table with some sample data) and created two tableadapters named Prods and Prods1 - the latter has a filter applied in the query to return slightly different results. I've dropped both tables on my form and both dutifully display their respective data.
I thought I would then swap the data source for each. So the default generated Window_Loaded:
MSDSTest.prodtestDataSet prodtestDataSet = ((MSDSTest.prodtestDataSet)(this.FindResource("prodtestDataSet")));
// Load data into the table Prods. You can modify this code as needed.
MSDSTest.prodtestDataSetTableAdapters.ProdsTableAdapter prodtestDataSetProdsTableAdapter = new MSDSTest.prodtestDataSetTableAdapters.ProdsTableAdapter();
prodtestDataSetProdsTableAdapter.Fill(prodtestDataSet.Prods);
System.Windows.Data.CollectionViewSource prodsViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("prodsViewSource")));
prodsViewSource.View.MoveCurrentToFirst();
// Load data into the table Prods1. You can modify this code as needed.
MSDSTest.prodtestDataSetTableAdapters.Prods1TableAdapter prodtestDataSetProds1TableAdapter = new MSDSTest.prodtestDataSetTableAdapters.Prods1TableAdapter();
prodtestDataSetProds1TableAdapter.Fill(prodtestDataSet.Prods1);
System.Windows.Data.CollectionViewSource prods1ViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("prods1ViewSource")));
prods1ViewSource.View.MoveCurrentToFirst();
I now want to make the first data grid (prodsViewSource) instead display the data for the second table, and ignore the second table entirely. So, I changed that as follows:
MSDSTest.prodtestDataSet prodtestDataSet = ((MSDSTest.prodtestDataSet)(this.FindResource("prodtestDataSet")));
// Load data into the table Prods. You can modify this code as needed.
MSDSTest.prodtestDataSetTableAdapters.Prods1TableAdapter prodtestDataSetProdsTableAdapter = new MSDSTest.prodtestDataSetTableAdapters.Prods1TableAdapter();
prodtestDataSetProdsTableAdapter.Fill(prodtestDataSet.Prods1);
System.Windows.Data.CollectionViewSource prodsViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("prodsViewSource")));
prodsViewSource.View.MoveCurrentToFirst();
With the second block having been commented out.
I must be missing something fundamental - what I think I'm doing is redefining the prodtestDataSetProdsTableAddapter variable to use an instance of the prods1 table adapter, and then using that to populate the prodsViewSource grid on the form, but I end up with a blank. Where's my error?
...
Well, I posted this after beating my head against it for an hour and, minutes later, realized the FAR easier thing to do is to just change the datacontext property of the grid in question.
I would still like to understand why doing it the vastly more complicated-bordering-on-Rube-Goldbergian way didn't work, though, so if anyone can explain that, it would still be welcome.

Binding a DataSet to a MultiColumn MS Chart

I have a DataSet with multiple DataTables that I'd like to bind to a chart with multiple column series. I expected each series to have different values respective to the DataTables in the DataSets; however, when the chart appears, both series display the same values in the Y column, the values from the first tables.
Here is a general idea what my code is like:
(I created and filled up a DataSet named diffCharts, diffCharts contains tables table1 and table2, both tables have columns Month and Amount)
Chart1.Series.Add("table1");
Chart1.Series["table1"].XValueMember = "Month";
Chart1.Series["table1"].YValueMembers = "Amount";
Chart1.Series.Add("table2");
Chart1.Series["table2"].XValueMember = "Month";
Chart1.Series["table2"].YValueMembers = "Amount";
Chart1.DataSource = diffCharts;
Chart1.DataBind();
Am I missing something? I've never bound a DataSet with multiple tables to a chart before.... Does MS know how to handle this?
I know this is an old post, but better late than never.
You can bind enumerable data sources directly to the DataPointCollection property in the series using the DataBind method. This way each series can be bound to a different table.
I am relatively new to C#, so there may be an adverse affect to this method that I am not aware of. It functions correctly in my limited testing.
MSDN link.
Chart1.Series.Add("table1");
Chart1.Series["table1"].Points.DataBind(table.AsEnumerable(), "nameofXaxis", "nameofYaxis, "nameofPropertyaxis");
Propertyaxis can be left as an empty string
I don't think you can do that.
You'll probably need to create a table with one column as the shared column for the X Axis (Month) and then two more columns for the Y values you want to graph.
The DataTable class has a Merge function that may do exactly what you need.

Creating a list of dataset fields and form fields for easy updating

I have a relatively complex dataset (numerous tables, some with multiple records) generated from reading in an XML file. I need to connect said dataset fields to an ASP form. At the moment I'm assigning them manually, like so in the pageload:
txt_first_init.Text = formData.ds.Tables["phrmHdrKey"].Rows[0]["first_init"].ToString();
txt_last_name.Text = formData.ds.Tables["phrmHdrKey"].Rows[0]["last_name"].ToString();
ddl_pregnancy_flag.SelectedValue = formData.ds.Tables["pPhrm"].Rows[0]["pregnancy_flag"].ToString();
And conversely when it's time to submit.
formData.ds.Tables["phrmHdrKey"].Rows[0]["first_init"] = txt_first_init.Text;
formData.ds.Tables["phrmHdrKey"].Rows[0]["last_name"] = txt_last_name.Text;
formData.ds.Tables["pPhrm"].Rows[0]["pregnancy_flag"] = ddl_pregnancy_flag.SelectedValue.ToString();
I did some looking into binding the textboxes (and dropdownlists, and checkboxes, and and and...) directly, but it seemed to be too many formats to use.
So this works fine, but as the number of fields increases, those load and unload lists are going to get unwieldy.
Trying to come up with a way to make a future update or addition so said list neater. I've gotten a very hand-wavy suggestion to place the two columns of names into a list, but unsure how to set such up, and how to load the items into a function that could evaluate and run the resulting commands.
Thoughts?
see here: Dynamic Form Generation in ASP.NET
looks like MS Dynamic Data is the answer.

Categories

Resources