2016-09-28 56 views
0

我正在用c#编写visual studio中的第一张图表。 我创建了一个图表和数据库,并创建它们之间的连接。 数据库有两列“DrukSensor”(float)和“DateTime”(DateTime)。C#图表系列错误

在我的代码中,我想创建一个图表,在x轴上的日期时间和在Y轴上的Druksensor。

但是,当我尝试我的代码它给出了一个错误:没有图表元素在系列集合中找到与名称“Druksensor”。

尝试浏览网页找到正确的anwser,但不幸找不到它。

这里是我的代码:

protected void Button1_Click(object sender, EventArgs e) 
    { 
     SqlConnection con = new SqlConnection(@"Data Source=LP12;Initial Catalog=SmmsData;Integrated Security=True"); 
     con.Open(); 
     SqlCommand cmd = new SqlCommand("Select DrukSensor,DateTime from SysteemSensorInfo2", con); 

     SqlDataReader myreader; 


     DataSet ds = new DataSet(); 
     new SqlDataAdapter(cmd).Fill(ds); 
     myreader = cmd.ExecuteReader(); 

     Chart1.Series["DrukSensor"].Points.AddXY(myreader.GetDateTime(Int32.Parse("DateTime")), myreader.GetFloat(Int32.Parse("DrukSensor"))); 
     Chart1.DataSource = ds; 
     Chart1.DataBind(); 
    } 

我希望有人能帮助我与我的第一张图的构建。

提前致谢!

回答

1
protected void Button1_Click(object sender, EventArgs e) 
    { 
     SqlConnection con = new SqlConnection(@"Data Source=LP12;Initial Catalog=SmmsData;Integrated Security=True"); 
     con.Open(); 
     SqlCommand cmd = new SqlCommand("Select DrukSensor,DateTime from SysteemSensorInfo2", con); 

     SqlDataReader myreader; 


     DataSet ds = new DataSet(); 
     new SqlDataAdapter(cmd).Fill(ds); 
     myreader = cmd.ExecuteReader(); 

     Chart1.Series.Add("DrukSensor"); // Add this line 

     Chart1.Series["DrukSensor"].Points.AddXY(myreader.GetDateTime(Int32.Parse("DateTime")), myreader.GetFloat(Int32.Parse("DrukSensor"))); 
     Chart1.DataSource = ds; 
     Chart1.DataBind(); 
    } 
+0

请不要在没有解释的情况下发布代码! – TaW

0

看起来你不是添加系列。 你可以这样做:

chart1.Series.Add("DrukSensor"); 

注意:请记住重画它。

chart1.Update();