从组合框中选择值后如何刷新表格

时间:2014-12-15 07:37:22

标签: sql vb.net vba localdb

我想知道是否可以在从组合框中选择另一个值后刷新当前的窗体,以便将该项目的详细信息显示在其他几个文本框中?

所以我的桌子看起来像 表名:程序 program_id program_name program_desc     1 T1 desc1

这是我正在使用atm的代码

Dim connection As New SqlClient.SqlConnection
    connection.ConnectionString = "pathway"
    Dim dr As SqlDataReader
    Dim prognamedesc As String
    Dim filetypetxt As String

    Dim prognamecombo As String
    Dim filetypecombo1 As String
    Dim command As New SqlCommand
    Dim querycommand As New SqlCommand
    connection.Open()
    'THIS SECTION LOADS DATA FROM THE TABLES'
    Try

        command.Connection = connection
        command.CommandType = CommandType.Text
        command.CommandText = "select program_name,filetype from program order by program_name; select * from filetype"
        querycommand.Connection = connection
        querycommand.CommandType = CommandType.Text
        querycommand.CommandText = "select program_name,program_desc , filetype from program where program_name like" & FiletypeComboBox1.SelectedItem & ""

        dr = command.ExecuteReader

        While dr.Read()

            prognamecombo = dr(0)
            Program_nameComboBox.Items.Add(prognamecombo)

        End While

        dr.NextResult()
        While dr.Read()
            filetypecombo1 = dr(0)
            FiletypeComboBox1.Items.Add(filetypecombo1)
            FiletypeComboBox1.SelectedItem = filetypecombo1
        End While
        dr.NextResult()
        While dr.Read()
            filetypetxt = dr(0)
            FiletypeLabel1.Text = filetypetxt

        End While
        dr.NextResult()
        While dr.Read()
            prognamedesc = dr(0)
            Program_descTextBox.Text = prognamedesc
        End While

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try





    connection.Close()

我想知道这是否可以使用当前代码?

1 个答案:

答案 0 :(得分:1)

要实现这一点,你必须做两件事,首先把你所有的代码放在一个方法中,并调用它像RefreshForm()

public void RefreshForm()
{
  // your code and binding goes here
}

第二步是通过在组合框上使用选定的索引更改事件,您只需调用包含所有绑定代码的方法。