Question ComboBox list before dragdown

Abu Sanaya

New member
Joined
Dec 14, 2009
Messages
2
Programming Experience
3-5

Hi dears,, this is my first thread with you,,

I am a beginner designer in vb.net,, I have designed a simple form,, added a menu with add, update, and delete the records,,

In the "delete" form,, I have added a combobox to list the records "StuID" from database,, it is successfully showing,, my query is when building the form the combobox appears empty unless you dragdown the list,, I want that the first record to be displayed directly so that the user knows that there are records in the conbo box,,

How to do that?!
 
In the Form_Load event use or after you have filled the dataSource:
VB.NET:
Combobox1.SelectedIndex = 0
 
In the Form_Load event use or after you have filled the dataSource:
VB.NET:
Combobox1.SelectedIndex = 0

Hi dear, where exactly to add this code?

PHP:
Imports System.Data.OleDb

Public Class Deleting
    Dim a As Integer
    Dim sqlstring As String

    Private Sub Deleting_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cn.Open()
        sqlstring = "Select * from Student"
        cmd = New OleDbCommand(sqlstring, cn)
        odr = cmd.ExecuteReader

        While odr.Read()
            ComboBox1.Items.Add(odr.GetValue(0))
        End While
        ComboBox1.SelectedIndex = 0
        cn.Close()
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        ComboBox1.SelectedIndex = 0
        a = Integer.Parse(ComboBox1.Text)
        cn.Open()
        sqlstring = "Select * from Student where StudID=" & a
        cmd = New OleDbCommand(sqlstring, cn)
        odr = cmd.ExecuteReader()
        odr.Read()
        Label1.Text = odr.GetValue(1)
        Label7.Text = odr.GetValue(2)
        Label8.Text = odr.GetValue(3)
        ComboBox1.SelectedIndex = 0
        cn.Close()
    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If MsgBox("Are you sure? Do you want to complete the delete process?", MsgBoxStyle.OkCancel + MsgBoxStyle.Question, "DELETE PROCESS") = MsgBoxResult.Ok Then
            cn.Open()
            sqlstring = "Delete from Student where StudID=" & a
            cmd = New OleDbCommand(sqlstring, cn)
            cmd.ExecuteNonQuery()
            MsgBox("Deleted successfully")
            cn.Close()
        End If

    End Sub
End Class
 
Somewhere after you load the CB, but not in the read loop.
VB.NET:
Private Sub Deleting_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cn.Open()
        sqlstring = "Select * from Student"
        cmd = New OleDbCommand(sqlstring, cn)
        odr = cmd.ExecuteReader

        While odr.Read()
            ComboBox1.Items.Add(odr.GetValue(0))
        End While
        cn.Close()
        ComboBox1.SelectedIndex = 0
End Sub
 
Back
Top