2010-07-23 30 views
2

我的圖表加載數據。圖表自動更新新的數據輸入從一個DataGridView

我想如果新值插入到DataGridView的自動更新我的新的數據圖表。

我的圖表在我的DataGridView中綁定到table1table2,它從DataTable獲取值。下面是代碼的一小部分:

 Dim myData As New DataTable 
     wcAdapter.SelectCommand = wcCommand 
     wcAdapter.Fill(myData) 

-

 Chart1.DataSource = myData 

     Chart1.Series("Series1").ValueMemberX = "table1" 
     Chart1.Series("Series1").ValueMembersY = "table2" 

下面是完整的代碼:

Try 
     wcconn.Open() 
     Dim wcCommand As New MySqlCommand() 
     ''telesales name 


     ' Dim wcQuery = "SELECT ID, Telesales, SUBSTRING(lastupdatedate, 1, 10) as 'Day', SUBSTRING(lastupdatetime FROM -9 FOR 6) as 'Time' FROM ratingout where Telesales='" & cbTelesales.Text & "' and lastupdatedate= '" & newDate & "' and lastupdatedate is not null and lastupdatetime is not null ORDER BY lastupdatetime ;" 
     ' wcCommand.Connection = wcconn 
     ' wcCommand.CommandText = wcQuery 

     Dim newDate As String 
     newDate = dateWorkCheck.Text 
     newDate = newDate.Replace("/", "-") 

     Dim y, m, d As String 
     y = newDate.Substring(6, 4) 
     m = newDate.Substring(3, 2) 
     d = newDate.Substring(0, 2) 

     newDate = y & "-" & m & "-" & d 

     Dim wcQuery = "SELECT ID, Telesales, lastupdatedate as 'Day', SUBSTRING(lastupdatetime FROM -8 FOR 2) as 'Time' FROM ratingout where Telesales='" & cbTelesales.Text & "' and lastupdatedate= '" & newDate & "' and lastupdatedate is not null and lastupdatetime is not null ORDER BY lastupdatetime ;" 
     wcCommand.Connection = wcconn 
     wcCommand.CommandText = wcQuery 


     Dim wcData As New DataTable 
     wcAdapter.SelectCommand = wcCommand 
     wcAdapter.Fill(wcData) 



     Dim i = 0 
     If wcData.Rows.Count = 0 Then 
      wcAdapter.Dispose() 
      Try 

       Dim wQuery = "SELECT ID, Telesales, lastupdatedate as 'Day', SUBSTRING(lastupdatetime FROM -8 FOR 2) as 'Time' FROM ratingout where Telesales='" & cbTelesales.Text & "' and lastupdatedate= '" & dateWorkCheck.Text & "' and lastupdatedate is not null and lastupdatetime is not null ORDER BY lastupdatetime ;" 
       wcCommand.Connection = wcconn 
       wcCommand.CommandText = wQuery 


       Dim wData As New DataTable 
       wcAdapter.SelectCommand = wcCommand 
       wcAdapter.Fill(wData) 

       wData.Columns.Add("tt") 
       wData.Columns.Add("num") 


       wcData.Columns.Add("tt") 
       wcData.Columns.Add("num") 
       'dgvWorkCheck.AutoSizeRowsMode = DataGridViewAutoSizeRowMode.AllCells 
       Dim dr As DataRow 
       For Each dr In wData.Rows 
        If lastV Is Nothing OrElse Not ColumnEqual(lastV, dr("Time")) Then 
         ''check if first value is nothing 
         If lastV = Nothing Then 
          lastV = "00" 
          l = "0" 
         Else 

          dr("tt") = lastV 
          dr("num") = l 
          'wcData.Tables("ratingout").Rows(I)("ID") = dr("ID") 
         End If 
         ListBox1.Items.Add(lastV & " <--> " & l) 
         lastV = dr("Time") 
         l = 1 
        ElseIf lastV Is Nothing OrElse ColumnEqual(lastV, dr("Time")) Then 
         l += 1 
         'Dim series1 As New Series() 
         'series1.Points.Add(l) 
        End If 


        For I = I To wData.Rows.Count 
         If I <> wData.Rows.Count Then 
          I += 1 
          If i = wData.Rows.Count Then 

           dr("tt") = lastV 
           dr("num") = l 

           ListBox1.BeginUpdate() 
           ListBox1.Items.Add(dr("Telesales") & " between[" & lastV & " and 17:00, ] <--> " & l & "[ records ]") 
           ListBox1.EndUpdate() 
          End If 

          GoTo n 
         Else 
          MsgBox("last data") 
         End If 
        Next 
n: 
       Next 
       txtRec.Text = wData.Rows.Count 

       dgvWorkCheck.DataSource = wData 

       ''chart 
       Dim ChartArea2 As ChartArea = New ChartArea() 
       Dim Legend2 As Legend = New Legend() 
       Dim Series2 As Series = New Series() 
       Dim Chart2 = New Chart() 
       Me.Controls.Add(Chart2) 

       ChartArea2.AxisX.LabelStyle.Angle = -90 
       ChartArea2.AxisX.LabelStyle.Interval = 1 

       ChartArea2.AxisY.LabelStyle.Angle = -90 
       ChartArea2.AxisY.LabelStyle.Interval = 5 

       ChartArea2.Name = "ChartArea2" 
       Chart2.ChartAreas.Add(ChartArea2) 
       Legend2.Name = "Legend2" 
       Chart2.Legends.Add(Legend2) 
       Chart2.Location = New System.Drawing.Point(12, 113) 
       Chart2.Name = "Chart2" 
       Series2.ChartArea = "ChartArea2" 
       Series2.Legend = "Legend2" 
       Series2.Name = "Series2" 
       Chart2.Series.Add(Series2) 
       Chart2.Size = New System.Drawing.Size(1145, 604) 
       Chart2.TabIndex = 0 
       Chart2.Text = "Chart2" 

       Chart2.Series("Series2").XValueMember = "tt" 
       Chart2.Series("Series2").YValueMembers = "num" 

       Chart2.DataSource = dgvWorkCheck.DataSource 

       Chart2.DataBind() 

      Catch ex As Exception 
       MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) 
      End Try 
      Exit Try 

回答

0

,因爲新的數據插入到數據庫中,只需要將gridview重新綁定到它的源代碼就可以顯示新的傳入數據。

你應該隔離在一個函數數據綁定到圖表的代碼,並把它稱爲每一個新的領域被插入時間:

Function FillChartWithData() 
    Dim myData As New DataTable 
    wcAdapter.SelectCommand = wcCommand 
    wcAdapter.Fill(myData) 
    ... 
    Chart1.Series("Series1").ValueMemberX = "table1" 
    Chart1.Series("Series1").ValueMembersY = "table2" 
End Function 

編輯

我看着你的編碼和看起來你缺少負責在'ratingout'表中插入新數據的部分。您應該創建一個可以讓你插入新的數據,沿着線的東西:

Dim insertRating = "INSERT INTO ratingout VALUES (@NewTeleSalesName, @NewDate);" 
Dim insertCmd As New MySqlCommand(insertRating, wcconn) 
insertCmd.Parameters.Add("@NewTeleSalesName", MySqlDbType.VarChar, 255, "teleSalesName") 
insertCmd.Parameters.Add("@NewDate", MySqlDbType.Datetime, 8, New DateTime(2010, 8, 5)) 
insertCmd.ExecuteNonQuery() 
+0

它不是一個網頁。它是一種vb形式。 – Gbolahan 2010-07-23 11:46:22

+0

當我更新時,我的datagridview獲取新記錄,但我的圖表不根據數據網格中的新數據繪製圖形。 我的datagridview和圖表都綁定到一個datable。 – Gbolahan 2010-07-23 12:03:26

+0

記錄插入後,您是否嘗試重新綁定圖表?您使用哪個gridview事件插入新數據? – jdecuyper 2010-07-23 12:11:22

0

爲了更新我的圖表bargraph命名爲CashChart(這是數據綁定到一個BindingSource)我必須做到以下幾點:

  1. 要清除圖表信息,

  2. 清除邊界源信息

  3. ,然後重新分配邊界源信息:例如:

    CashChart.Series(0).Points.Clear() 
    CashChart.DataSource = ""      
    CashChart.DataSource = ESTADOINSTANTANEOBindingSource 
    

之前,只有我DataTable被更新,但這些命令後,我能得到bargraph用新值更新在桌子裏。