这是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中显示错误。我也尝试了另一种方法,但我无法处理它。
答案 0 :(得分:3)
如果您要做的就是绑定数据表,那么就这样做:
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
时,您必须使用“UserName”(xField
)。不是Value
或Sum(Value)
。
答案 1 :(得分:0)
检查读者的价值观......如果你有价值观,
尝试替换
chart1.DataBindTable(reader,"sum(Value)");
带
chart1.DataBindTable(reader,"Value");
一旦将值汇总到查询中,您无需再次提及总和,您的Value参数将具有总和
编辑---更新了代码..
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();
while (reader.Read())
{
chart1.DataBindTable(reader, reader["Value"]);
}
}
foreach (Series series in chart1.Series)
{
series.CustomProperties = "DrawingStyle=LightToDark";
}
}