[英]C# Chart series error
我正在使用C#在Visual Studio中構建我的第一個圖表。 我創建了一個圖表和數據庫,並在它們之間建立了聯系。 該數據庫具有兩列“ DrukSensor”(浮動)和“ DateTime”(DateTime)。
在我的代碼中,我想創建一個圖表,在x軸上使用DateTime,在Y軸上使用Druksensor。
但是,當我嘗試我的代碼時,出現以下錯誤:在seriescollection中找不到名稱為“ Druksensor”的Chart元素。
試圖在網上沖浪找到合適的弓箭手,但不幸的是找不到它。
這是我的代碼:
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();
}
我希望有人能幫助我建立第一個圖表。
提前致謝!
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();
}
看起來您沒有添加該系列。 您可以執行以下操作:
chart1.Series.Add("DrukSensor");
注意:記得重繪它。
chart1.Update();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.