Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Datatable ADD COLUMN NAMES
table1.Columns.Add(" ÜRÜN CİNSİ", GetType(String))
table1.Columns.Add("STOK KODU", GetType(String))
table1.Columns.Add("BİRİM FİYAT/KG", GetType(Integer))
table1.Columns.Add("KDV ORANI", GetType(Integer))
' DAtatable ADD ROWS
table1.Rows.Add("ERİK", "Y-9532", 1250, 6)
table1.Rows.Add("KİRAZ", "Z-1250", 1500, 8)
table1.Rows.Add("ÇİLEK", "H-2548", 2000, 8)
'ADD TO combobox
ComboBox1.Items.Add("ERİK")
ComboBox1.Items.Add("KİRAZ")
ComboBox1.Items.Add("ÇİLEK")
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
Dim Table_ndx As Integer
For index = 0 To ComboBox1.Items.Count
If (ComboBox1.SelectedItem == table1.Rows(index)(0)) Then
Table_ndx = index
End If
Next
Me.Text = Table_ndx.ToString
lbl0_stok_kodu.Text = table1.Rows(Table_ndx)(1) 'stkkodu
tb1_birim_kg.Text = table1.Rows(Table_ndx)(2) 'birim kg
tb2_kdv_orani.Text = table1.Rows(Table_ndx)(3) 'kdv oranı
" Table_ndx" 总是来零,因为如果阻止它就不会进入
我已经在combobox中有过物品 并将它们加载到数据表中
更新我将其更改为single =但代码仍然运行 Table_ndx等于ZERO
答案 0 :(得分:1)
VB.NET不知道==
是什么。
应该是:(假设您的combox包含对象作为table1.Rows(0)(索引)
If (ComboBox1.SelectedItem = table1.Rows(index)(0)) Then
...
End If
答案 1 :(得分:1)
你的问题是双重的。首先,正如已经指出的那样,你的比较应该是
If (ComboBox1.SelectedItem = table1.Rows(index)(0)) Then
其次,你没有错误检查,如果你这样做,你会发现从0迭代到Items.Count从0变为3.你的数据表中没有3的索引,所以代码炸弹了它永远不会打到你的其余代码。将其更改为以下内容......
For index = 0 To ComboBox1.Items.Count - 1
永远记住VB.NET使用基于0的索引,因此您只能迭代到MaxArraySize - 1.