本文介绍了如何将数据表绑定到MS图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是myCode:

 private void frmChart_Load(object sender, EventArgs e)
    {
        string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
        using (SqlConnection Con = new SqlConnection(cs))
        {
            SqlCommand cmdSum = new SqlCommand("Select distinct(UserName),sum(Value) from mytable  group by UserName",Con);
            Con.Open();
            SqlDataReader reader = cmdSum.ExecuteReader();
            chart1.DataBindTable(reader,"sum(Value)");
        }
        foreach (Series series in chart1.Series)
        {
            series.CustomProperties = "DrawingStyle=LightToDark";
        }

    }

它向我显示了chart1.DatabindTable中的错误.我也尝试了另一种方法,但是我无法处理它.

It shows me an error in chart1.DatabindTable. also I try another method but I could not handle it.

推荐答案

如果您要做的只是绑定数据表,则只需执行以下操作:

If all you're trying to do is to bind a data table, then just do this:

        private void Form1_Load(object sender, EventArgs e)
        {
            string sql = "your sql here";

            SqlDataAdapter adapter = new SqlDataAdapter(sql, connectionString);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            chart1.DataBindTable(dt.DefaultView, "UserName");
        }

请注意,在调用DataBindTable时,必须使用用户名"(xField).不是ValueSum(Value).

Note when calling DataBindTable you have to use "UserName" (xField). Not Value or Sum(Value).

这篇关于如何将数据表绑定到MS图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 22:02