gpt4 book ai didi

mysql - 如何计算、比较和添加 DataGridView 行中的值?

转载 作者:行者123 更新时间:2023-11-29 18:28:13 25 4
gpt4 key购买 nike

我需要什么才能从 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 中的所有现有数据)

谢谢!

最佳答案

我已经找到了答案

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

关于mysql - 如何计算、比较和添加 DataGridView 行中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45978114/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com