2016-09-28 57 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();