下拉列表与两个SQL表的连接 [英] Connectivity of Dropdown lists with two tables of SQL

查看:87
本文介绍了下拉列表与两个SQL表的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是:我的页面中有两个下拉列表,并且都将与sql中的两个单独的表绑定.但是存在一个问题,即dropdownlist1的选定值将过滤dropdoenlist2的内容.

即,例如,如果一个dropdownlist(state)具有状态名称,并且在将记录插入表中时自动生成状态ID,则基于此记录ID(整数类型),我将如何填充已过滤的dropdownlist2(city)

Ques: There are two dropdown lists in my page and both are to be binded with two separate Tables in sql. But there is a problem that, the selected value of dropdownlist1 will filter the contents of dropdoenlist2.

I.e For example, if one dropdownlist(state) is having names of states, and state id is auto generated while inserting records in table, on the basis of this record id, which is integer type, how will i fill dropdownlist2(cities) filtered by the stateID present in state table and city table.

推荐答案

您正在尝试级联下拉列表.现在,由于您刚刚将其标记为C#,因此我们假设您要在Winforms中而不是ASP.NET中使用它. (即使您使用dropddownlist单词!)

步骤,
1.将您的dropdown1绑定到从数据库获取的数据表.
例如:
You are trying to have Cascading dropdown. Now, since you have just tagged it as C#, we assume you want this in Winforms and not in ASP.NET. (Even though you use dropddownlist word!)

Steps,
1. Bind your dropdown1 to datatable fetched from DB.
Ex:
SELECT StateID, StateName FROM MstStates


2.为dropdown1定义了下拉更改事件
3.在此selectedindexchange事件中,获取选定的状态ID,并将其用作获取下拉列表2的数据的过滤器
例如:


2. Have dropdown change event defined for dropdown1
3. In this selectedindexchange event, get the selected state ID and use this as a filter for getting data for dropdown 2
Ex:

SELECT CityID, CityName FROM MstCity WHERE RelatedStateID = 5


4.将此查询数据绑定到您的dropdown2

这会将两个下拉列表与两个SQL表绑定在一起,并具有所需的过滤器相关条件.


4. Bind this query data to your dropdown2

This will bind the two drodowns with two SQL tables and also have the filter dependent criteria you want.


string queryState="select stateid,statename from statemaster";

//write code for filling dataset     

       ddlstate.DataSource = dsState;
       ddlstate.DataTextField = statename ;
       ddlstate.DataValueField = stateid;
       ddlstate.DataBind();
       ddlstate.Items.Insert(0, "Select..");
       ds.Clear();



//记住状态下拉菜单的



//remember for

autopostback=true



of dropdown of state

string queryCity="select cityid,cityname from citymaster where stateid=" + ddlstate.selectedvalue;

//write code for filling dataset dsCity

       ddlCity.DataSource = ds;
       ddlCity.DataTextField = dataTextField;
       ddlCity.DataValueField = dataValueField;
       ddlCity.DataBind();
       ddlCity.Items.Insert(0, "Select..");
       dsCity.Clear();




希望你现在可以做...我给了你更好的提示..

如果正确,请回答...




hope you can do now...i had given somewhat better hints to you..

make it answer if you got it right...


这篇关于下拉列表与两个SQL表的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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