如何在DataGridView的行中计算,比较和添加值?

时间:2017-08-31 09:35:01

标签: mysql vb.net datagridview

为了能够从DataGridView中计算,比较和添加值,我需要什么?

这是我用于插入/填充Datagridview

的代码行
Sub display()
    Dim temp As Double = 0
    'count is my ID that counts the number of tickets from 1 and so on - count is also primary key in my database..
    Dim lt As String = "select count as Tickets, mname as Movie, mtime as Time, mprice as Price, mimg as Photo from tickets order by count asc" 'add desc for descending order/ asc for ascending (order by vlanme desc)
    Dim da As New MySqlDataAdapter(lt, con)
    con.Open()

    Dim ds As New DataSet
    da.Fill(ds, "settings")
    da.Dispose()
    DataGridView1.DataSource = ds.Tables(0)
    con.Close()
End Sub

我想要的是计算我的Datagridview有多少行

我还想比较两行之间的值

我还想添加两行的值

我只想计算我有多少票,比较行(1)和行(2)的名称等等,然后从行(1)中添加价格等等总计

(我想在不使用.SelectedCell方法的情况下这样做,因为我会计算,比较,一次添加所有数据 - datagridview中的所有现有数据)

谢谢!

1 个答案:

答案 0 :(得分:0)

我已经找到了答案

Dim bs As New BindingSource
    Dim dr As MySqlDataReader
    Dim rowcount As Integer = DataGridView1.Rows.Count
    Dim moviecount As Integer = 0
    Dim price As Integer = 0
    For index As Integer = 0 To rowcount
        Try
            If con.State = ConnectionState.Closed Then
                con.Open()
                Dim query As String = "select * from tickets where count = '" & index & "'"
                Dim cmd As New MySqlCommand(query, con)
                dr = cmd.ExecuteReader
                While dr.Read
                    If moviecount = 0 Then
                        movie = dr.GetString("mname")
                        moviecount += 1
                    End If

                    If moviecount > 0 Then
                        If movie = dr.GetString("mname") Then
                            price += dr.GetInt32("mprice")
                        Else
                            MsgBox(movie & " has a total sales of : " & price, MsgBoxStyle.Information, "System Checked!")
                            price = 0
                            price += dr.GetInt32("mprice")
                            moviecount = 0
                        End If
                    End If
                End While
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        con.Close()
    Next
    MsgBox(movie & " has a total sales of : " & price, MsgBoxStyle.Information, "System Checked!")

dr.getString(“来自数据库的变量”)或dr.getInt32(“来自数据库的变量”)

这使您可以选择将数据库中的哪个值传输到您设置为String或Int的Varname(如果添加循环,还需要哪个行)