VB.NET来自2个表的数据绑定 [英] VB.NET Databinding from 2 Tables

查看:73
本文介绍了VB.NET来自2个表的数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下结构

|-Table 1
|--PK IDT1
|--Name field
|-Table 2
|--PK ID
|--FK IDT1
|--Name field

我想将表1绑定到combobox1,将表2绑定到combobox2,这样当有人在combobox1中选择一个值时,combobox2仅填充FK中的行来自combobox1的IDT1值。

I want to bind Table 1 to combobox1 and Table 2 to combobox2 so as when someone selects a value in combobox1, combobox2 populates with only the rows with the FK IDT1 value from combobox1. Is this possible with databinding or do I have to code it?

推荐答案

FORM_LOAD 仅填充 ComboBox1 。此伪代码可能会帮助您:

on the FORM_LOAD of your application just populate only ComboBox1. this psuedocode may help you:

在您的应用程序上进行表单加载:

ON FORM_LOAD of your Application:

      Bind ComboBox1
      SET DISPLAY MEMBER Property of BomboBox1 to field
      SET VALUE MEMBER Property of ComboBox1 to IDT1
      Clear ComboBox1 current Text

ComboBox1

      Bind ComboBox2 using the following Query:
           SELECT ID, field FROM Table2 WHERE IDT1 = ComboBox1.SelectedValue.ToString
      SET DISPLAY MEMBER Property of ComboBox2 to field
      SET VALUE MEMBER Property of ComboBox2 to IDT

SelectedIndexChanged 每次SelectedIndex属性值更改时,都会引发事件。如果您的ComboBox最初为空,则其SelectedIndex为-1。当您将数据绑定到它时,默认情况下会选择第一项,并且SelectedIndex更改为0。然后,您再次将SelectedIndex显式设置为-1。

The SelectedIndexChanged event is raised every time the SelectedIndex property value changes. If your ComboBox is initially empty then its SelectedIndex is -1. When you bind data to it the first item is selected by default and the SelectedIndex changes to 0. You are then explicitly setting the SelectedIndex to -1 again.

仅对知道用户何时进行选择然后处理 SelectionChangeCommitted 事件感兴趣。

If you're only interested in knowing when the user makes a selection then handle the SelectionChangeCommitted event instead.

这篇关于VB.NET来自2个表的数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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