Question

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ı

This "Table_ndx" always comes ZERO because, it doesn't get in if block

I have already had items in combobox and have loaded them in datatable too

update i changed it to single = but code runs BUT still Table_ndx is equal ZERO

Était-ce utile?

La solution

Your problem is twofold. First, as has been pointed out, your comparison should be

If (ComboBox1.SelectedItem = table1.Rows(index)(0)) Then

Second, you have no error checking, if you did, you would find out that iterating from 0 to Items.Count goes from 0 to 3. There is no index of 3 in your data table, so the code bombs out and it never hits the rest of your code. Change that to the following...

For index = 0 To ComboBox1.Items.Count - 1

Always remember that VB.NET uses 0-based indexing, so you only ever iterate to MaxArraySize - 1.

Autres conseils

VB.NET doesn't know what == is.

Should be: (assuming your combox contains the object as the table1.Rows(0)(index)

 If (ComboBox1.SelectedItem = table1.Rows(index)(0)) Then 
   ...
 End If
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top