繁体   English   中英

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

[英]How can I Count, Compare and Add Values in DataGridView`s Rows?

我需要什么才能能够对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有多少行

我也想比较两行之间的值

我也想添加两行的值

我只想计算我有多少张票,比较Row(1)和Row(2)的名称,依此类推,然后将Row(1)的价格加起来,依此类推

(我想在不使用.SelectedCell方法的情况下执行此操作,因为我将一次计数,比较,添加它们-datagridview中的所有现有数据)

谢谢!

我已经找到答案了

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中(如果添加循环,还需要哪一行)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM