VB.Net-SQL-在列表框上选择需要更新的文本框-无数据绑定 [英] VB.Net - SQL - On listbox select need textbox to update - no databinding

查看:72
本文介绍了VB.Net-SQL-在列表框上选择需要更新的文本框-无数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧...

我已尽我所能将这些代码拼凑在一起,以使其正常工作.这是带有SQL数据源的Windows窗体应用程序.此表单(frmAdvocates)在表单左侧具有一个列表框.我拥有的代码(在打开的表单上)将连接到数据库,并使用名字和姓氏填充列表框,例如– NameLast& ",& NameFirst.看来还可以.

我遇到的问题是,我知道需要"SelectedIndexChanged"的代码,以便当用户从列表框中选择一个名称时,同一表格(名称,地址,电话等)上的相应文本框将被数据填充.

我使用数据绑定开始了这个项目,但想摆脱使用实际代码进行绑定的问题.

任何帮助将不胜感激.

谢谢!

Ok...

I have pieced together this code as best I could to make it work. This is a Windows Form application with an SQL data source. This form (frmAdvocates) has a listbox on the left side of the form. The code I have will (on form open) connect to the database and populate the listbox with the first and last name as – NameLast & ", " & NameFirst. It seems to work ok.

The problem I have is that I know need code for the "SelectedIndexChanged" so that when the user selects a name from the listbox the appropriate text boxes on the same form (name, address, phone, etc..) are filled with data.

I started this project using data binding but wanted to get away from it binding to using actual code.

Any assistance would be appreciated.

Thank you!

Private Sub frmAdvocates_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ''Clear items in listbox
        lbxVictimAdvocates.Items.Clear()

        ''Create a Connection object.
        myConn = New SqlConnection("Initial Catalog=VictimAdvantage_db;" & _
                "Data Source=WALSH\SQLEXPRESS;Initial Catalog=VictimAdvantage_db;Integrated Security=True")

        ''Create a Command object.
        myCmd = myConn.CreateCommand
        myCmd.CommandText = "SELECT NameFirst, NameMidInt, NameLast, ID, Status FROM tblAdvocates WHERE Status=''Active'' ORDER BY NameLast"

        ''Open the connection.
        myConn.Open()

        ''Fill reader
        myReader = myCmd.ExecuteReader()

        ''Concatenate the query result into a string.
        Do While myReader.Read()
            results = results & myReader.GetString(2) & ", " & myReader.GetString(0) & " " & myReader.GetString(1) & vbLf
            lbxVictimAdvocates.Items.Add(myReader.GetString(2) & ", " & myReader.GetString(0) & vbLf)
        Loop

        ''Check for DOB entry and calculate age or leave empty
                If txtDOB.Text = String.Empty Then
                    txtAge.Text = ""
                Else
                    txtAge.Text = DateDiff(DateInterval.Year, CDate(txtDOB.Text.Trim), Now.Date)
                End If

        ''Display proper name on Advocate list groupbox
                gbxAdvocates.Text = "Advocates - Active"
                gbxAdvocates.ForeColor = Color.DarkGreen

        ''Check for email address
        If txtEmail.Text = String.Empty Then
            btnSendEmail.Enabled = False
        Else
            btnSendEmail.Enabled = True
        End If
    End Sub

推荐答案

我不是为什么你不想使用数据绑定,而是这里的整个思想是为列表并在选中此人以获取其信息时将其获取
无论如何,在填充列表的同时要做一个通用字典,该字典包含人的索引和唯一键(数据库中的Ex.personId)

I don''t why you don''t want to use databinding but the whole idea here to keep a unique key for each item in the list and get it when this person been selected to get his information
Anyway to do that while filling the list make a general dictionary holding the index of the person and the unique key (Ex.personId in Database)

Dim list as new Dictionary<int, string>()





Dim  itemIndex as intger  = 0
           Do While myReader.Read()
          results = results & myReader.GetString(2) & ", " & myReader.GetString(0) & " " & myReader.GetString(1) & vbLf
          lbxVictimAdvocates.Items.Add(myReader.GetString(2) & ", " & myReader.GetString(0) & vbLf)
              list.Add (itemIndex , UniqueId  )
                  itemIndex = itemIndex + 1
      Loop



当您想从selectedIndexchanged调用它时



And when you want to call it from selectedIndexchanged

MessageBox.Show(list[listBox1.SelectedIndex])


这篇关于VB.Net-SQL-在列表框上选择需要更新的文本框-无数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆