Vb.net 我怎样才能把一个额外的价值?
我有这样一个表(Vb.net 我怎样才能把一个额外的价值?,vb.net,datagridview,Vb.net,Datagridview,我有这样一个表(DataGridView): Col1 | Col2 | Col3 3 | Mars | Regular 这是我的密码: For a As Integer = 0 To Form3.DataGridView1.Rows.Count - 1 For b As Integer = 0 To Form3.DataGridView1.Rows.Count - 1 For c As Integer = 0 To Form3.DataGridView1.Ro
DataGridView
):
Col1 | Col2 | Col3
3 | Mars | Regular
这是我的密码:
For a As Integer = 0 To Form3.DataGridView1.Rows.Count - 1
For b As Integer = 0 To Form3.DataGridView1.Rows.Count - 1
For c As Integer = 0 To Form3.DataGridView1.Rows.Count - 1
If Form3.DataGridView1.Rows(c).Cells(2).Value = "Regular" Then
If Form3.DataGridView1.Rows(b).Cells(1).Value = Form3.MetroComboBox7.Items(0) Then
fair = 7 * Form3.DataGridView1.Rows(a).Cells(0).Value
Label1.Text += fair
End If
End If
Next
Next
Next
我想设置,如果在
Col3
上选择Regular,在Col2
上选择Mars,则值为7,它将乘以行Col1
,并且每行都相同。我认为应该使用事件dtgv.CellMouseClick
然后你创造你想要的条件。我举一个例子:
Public Sub event_select() Handles dtgv.CellMouseClick
Dim row As Integer = dtgv.CurrentRow.Index()
' If the column 2 and 3 are selected
If dtgv.Rows(row).Cells(1).Selected = True And dtgv.Rows(row).Cells(2).Selected = True Then
' If the value of the 2nd column is Mars and the value of the 3rd column is Regular
If dtgv.Rows(row).Cells(1).Value = "Mars" And dtgv.Rows(row).Cells(2).Value = "Regular" Then
Label1.Text = 7 * dtgv.Rows(row).Cells(0).Value
End If
End If
End Sub
您还应该检查是否没有选择其他行的单元格。所有行的一个循环足以计算所有行的“公平”
Const REGULAR_VALUE As String = "Regular"
Const FAIR_COEFFICENT As Integer = 7
Dim fairSum As Integer = 0
For Each row As DataGridViewRow in DataGridView1.Rows
If REGULAR_VALUE.Equals(row.Cells(2).Value.ToString()) = False Then Continue For
If Equals(MetroComboBox7.Items(0), row.Cells(1).Value) = False Then Continue For
Dim col1Value As Integer = Integer.Parse(row.Cells(1).Value)
Dim fair As Integer = col1Value * FAIR_COEFFICENT
fairSum += fair
Next
Label1.Text = fairSum.ToString()
并在项目中或至少在代码文件(文件的第一行)中设置选项Strict On
。这将通过在编译时提供有关可能的类型转换错误的快速反馈来节省时间 创建以强类型方式表示数据的类
Public Class Ticket
Public Property Passenger As Integer
Public Property Destination As String
Public Property Status As String
End Class
然后,您可以在表单中以简单的方式向DataGridView添加行
Public Class YourForm
Private _tickets As New BindigList(Of Ticket)()
Public Sub New()
InitializeComponent() ' Forms required method
DataGridView1.DataSource = _tickets
End Sub
Private Sub Populate(passenger As Integer, destination As String, Status As String)
Dim newTicket As New Ticket With
{
.Passenger = passenger,
.Destination = destination,
.Status = Status,
}
_ticket.Add(newTicket)
End Sub
'Then you can loop all rows with correct types
Private Sub Calculate()
Dim fairSum As Integer = 0
For Each ticket As Ticket in _tickets
If REGULAR_VALUE.Equals(ticket.Status) = False Then Continue For
If ticket.Destination.Equals(MetroComboBox7.Items(0)) = False Then Continue For
Dim fair As Integer = ticket.Passenger * FAIR_COEFFICENT
fairSum += fair
Next
Label1.Text = fairSum.ToString()
End Sub
End Class
为什么要在
DataGridView
行中循环三次?你不应该删除b的循环和c的循环,并像这样引用a
。行(a)
?我正在尝试循环3列中的每个值,你不需要这样做。只需对As Integer循环使用,并在所有行(a)
上引用a
。摆脱b
的和c
的循环。如何在没有事件的情况下做到这一点?加载时加载表单时,您没有选定的单元格,对吗?您也可以将表单3的事件触发到其他表单,并显示“指定的强制转换无效”。您应该给他错误发生的行@NeCatDim col1Value As Integer=DirectCast(row.cells(0).Value,Integer)。这行显示“指定的强制转换无效”@NeCat第一列中的值的类型是什么?私有子填充(乘客为字符串,目的地为字符串,状态为字符串)Dim row As String()=New String(){乘客,目的地,状态}DataGridView1.Rows.Add(row)这是我用于每列的方法,列1的组合框具有整数值