如何在 c#.net 中创建图形

How to create graph in c#.net

ChartArea ChartArea1 = new ChartArea();
Legend Legend1 = new Legend();
Series Series1 = new Series();
dynamic Chart1 = new Chart();
this.Controls.Add(Chart1);

ChartArea1.Name = "ChartArea1";
Chart1.ChartAreas.Add(ChartArea1);
Legend1.Name = "Legend1";
Chart1.Legends.Add(Legend1);
Chart1.Location = new System.Drawing.Point(210, 241);
Chart1.Name = "Chart1";
Series1.ChartArea = "ChartArea1";
Series1.Legend = "Legend1";
Series1.Name = "Series1";
Chart1.Series.Add(Series1);
Chart1.Size = new System.Drawing.Size(837, 346);
Chart1.TabIndex = 0;
Chart1.Text = "Chart1";

Chart1.Series("Series1").XValueMember = "subjects";
Chart1.Series("Series1").YValueMembers = "srno";

Chart1.DataSource = ds.Tables("create_exam_next");
Chart1.BringToFront();

我使用上面的代码,但它给出了 'dynamic' 关键字的错误。我使用 MSchart。它给出了错误,因为我使用 visual studio 2008 和 .net框架 3.5.

C# 关键字 dynamic 仅在 .Net 4.0 (see basic instroduction) 以上可用。

我强烈建议您将您的项目升级到 .Net 4.5 和 Visual Studio 2013(有免费版本),除非您的公司或学校禁止它。

要快速修复,请将 dynamic 行更改为 Chart,如下所示。

Chart Chart1 = new Chart();