2017-04-14 62 views
0

古鲁的请halp这个可怜的padawan。C#数据图表x轴显示多个城市windows窗体

我想显示一个图表,我给我的数据库中的值。在X轴上,我只想要一个叫做“鹿特丹”的城市,但它在其他行的X轴上给了我多次鹿特丹。

The chart where I am talking about

这是代码

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form2 : Form 
    { 
     public Form2() 
     { 
      InitializeComponent(); 
     } 

     private void Form2_Load(object sender, EventArgs e) 
     { 
      // TODO: This line of code loads data into the 'official_DatabaseDataSet.fietsdiefstal' table. You can move, or remove it, as needed. 
      this.fietsdiefstalTableAdapter.Fill(this.official_DatabaseDataSet.fietsdiefstal); 

     } 

     private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) 
     { 

     } 

     private void button2_Click(object sender, EventArgs e) 
     { 
      chart1.Series["Fietsdiefstal"].XValueMember = "Plaats"; 
      chart1.Series["Fietsdiefstal"].YValueMembers = "Begintijd"; 







      chart1.DataSource = official_DatabaseDataSet.fietsdiefstal; 
      chart1.DataBind(); 

     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       fietsdiefstalBindingSource.EndEdit(); 
       fietsdiefstalTableAdapter.Update(official_DatabaseDataSet.fietsdiefstal); 
       MessageBox.Show("Your data has been succesfully saved.", " Message", MessageBoxButtons.OK, MessageBoxIcon.Information); 
      } 
      catch(Exception ex) 
      { 
       MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      } 
     } 

     private void chart1_Click(object sender, EventArgs e) 
     { 

     } 

     private void fillByToolStripButton_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       this.fietsdiefstalTableAdapter.FillBy(this.official_DatabaseDataSet.fietsdiefstal); 
      } 
      catch (System.Exception ex) 
      { 
       System.Windows.Forms.MessageBox.Show(ex.Message); 
      } 

     } 

     private void time1300ToolStripButton_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       this.fietsdiefstalTableAdapter.Time1300(this.official_DatabaseDataSet.fietsdiefstal); 
      } 
      catch (System.Exception ex) 
      { 
       System.Windows.Forms.MessageBox.Show(ex.Message); 
      } 

     } 

     private void timeToolStripButton_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       this.fietsdiefstalTableAdapter.Time(this.official_DatabaseDataSet.fietsdiefstal, param1ToolStripTextBox.Text); 
      } 
      catch (System.Exception ex) 
      { 
       System.Windows.Forms.MessageBox.Show(ex.Message); 
      } 

     } 

     private void param1ToolStripLabel_Click(object sender, EventArgs e) 
     { 

     } 

     private void straatToolStripButton_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       this.fietsdiefstalTableAdapter.Straat(this.official_DatabaseDataSet.fietsdiefstal, param1ToolStripTextBox1.Text); 
      } 
      catch (System.Exception ex) 
      { 
       System.Windows.Forms.MessageBox.Show(ex.Message); 
      } 

     } 
    } 
} 
+1

请花一些时间来阅读[提问]的文档(http://stackoverflow.com/help/asking)。您应该提供更多关于您的问题和[最小,完整和可验证示例]的详细信息(https://stackoverflow.com/help/mcve)。 – MrZander

+0

现在好点了吗? –

+0

很多!我会删除我的downvote :) – MrZander

回答

1

因此,它看起来像你正在使用你的DataGrid和图表两个fietsdiefstal数据。

您应该创建第二个数据集,将您的结果汇总为您所需的格式。你可以任何一种语言,你使用的填充数据表(我假设SQL?)

因此,例如,在SQL查询应该是这个样子

SELECT City, SUM(SomeValue) FROM MyTable 
GROUP BY City 
在C#或做

其中City是您将用于填充X轴的列,SomeValue是酒吧的高度。

如果您想在C#中执行此操作,您将需要遍历记录。喜欢的东西:

var aggregateData = new Dictionary<string, int>(); 
foreach(DataRow row in YourDataTable.Rows) 
{ 
    int val; 
    aggregateData.TryGetValue(row["City"], out val); 
    aggregateData[row["City"]] = val + row["SomeValue"]; 
} 

(我没有测试这些示例)

+0

该死的你说我在想什么,但不能确定我怎么能做到这一点。今晚我会检查它是否有效!谢谢! –

+0

这很难.... –