我需要帮助来绑定Asp.Net数据图表

问题描述:

您好,

我想从SQL数据表中绑定数据图表。下面是我正在使用的代码。

Hello,
I want to bind data chart from the SQL Data table. Below is the code that I am using.

private string QueryString = "SELECT [Qty], [Hour], [ObjectifTotal] FROM [TRGCharts]";
  SqlConnection conn = new SqlConnection(ConnectionString);
           SqlCommand cmd = new SqlCommand(QueryString, conn);

           SqlDataAdapter da = new SqlDataAdapter();
           da.SelectCommand = cmd;
           DataTable dt = new DataTable();
           da.Fill(dt);
           Chart1.DataSource = dt;
           Chart1.Series["Series1"].XValueMember = dt.Columns["Hour"].ToString();
           Chart1.Series["Series1"].YValueMembers = dt.Columns["Qty"].ToString();
           Chart1.Series["Series2"].XValueMember = dt.Columns["Hour"].ToString();
           Chart1.Series["Series2"].YValueMembers = dt.Columns["ObjectifTotal"].ToString();
           Chart1.DataSource = dt;
           this.Chart1.Series["Series1"].Points.AddXY(dt.Columns["Hour"].ToString(), dt.Columns["Qty"].ToString());
           this.Chart1.DataBind();
           this.Chart1.Series["Series2"].Points.AddXY(dt.Columns["Hour"].ToString(), dt.Columns["ObjectifTotal"].ToString());
           this.Chart1.DataBind();



我的问题是它总是绑定Series1.....如果我做错了请告诉我......

任何人帮我绑定多个系列的1个图表...


My problem is the it always bind the "Series1".....Please let me know if I was doing anything wrong...
Anybody help me to bind 1 Chart with multiple series...

您的代码是正确的,代码中没有问题。如果在输出中没有看到Series2,则ObjectifTotal的值可能为零。检查从数据库中获得的值。



您的代码中的一个问题是您正在调用两次DataBind()方法,您只需要调用一次最后,删除额外的声明。 DataSource属性
Your code is correct, there is no issue in your code. If you don't see Series2 in the output, perhaps the value of ObjectifTotal is zero. Check the values that you get from the database.

One problem in your code is that you are calling DataBind() method twice, you need to call it only once at the end, remove the extra statement. Same is the case for DataSource property