vb.net - 使用新数据输入自动更新图表

标签 vb.net datagridview charts

我的图表从 DataGridView 加载数据。

如果新值插入到 DataGridView 中,我想用新数据自动更新我的图表。

我的图表绑定(bind)到我的 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

最佳答案

由于新数据被插入到数据库中,您只需将 gridview 重新绑定(bind)到它的源以显示新传入的数据。

您应该在函数中隔离将数据绑定(bind)到图表的代码,并在每次插入新字段时调用它:

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()

关于vb.net - 使用新数据输入自动更新图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3317718/

相关文章:

vb.net - 如何每隔X分钟在VB.NET中调用一个函数?

c# - 将自定义列表属性绑定(bind)到 DatagridView

wpf - 使用 datatriggers 和 IValueConverter 更新 datagridview 行背景颜色

c# - 右键单击以选择 Datagridview 中的一行并显示一个菜单以将其删除

excel - 无法更改自定义错误栏的宽度

javascript - 图表不显示

javascript - Highcharts 水平同步图表

.net - 使用VB.NET上传文件到SFTP服务器

vb.net - Dapper ORM 分页和排序

vb.net - VB.NET 中的新行