繁体   English   中英

C#图表系列错误

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM