根据下拉列表值从数据库中选择值 [英] Select value from database based on dropdownlist value

查看:212
本文介绍了根据下拉列表值从数据库中选择值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有数据库表leave_rec(name,date1,leave,ltype),下拉列表和gridview。

我想这样做当我在下拉列表中选择月份(例如2月)时,gridview应该仅显示fabruary月份的所有表值(egrohan leuva,2/28/2013,full,casual),表示月份= 2(2月)的记录。如何克服这个问题?我试过,但我现在只能在gridview中显示所有的值。任何帮助都会受到极大的关注。



Hello everyone,
I have database table leave_rec(name,date1,leave,ltype),Dropdown list,and a gridview.
I want to do such that,when i select month(e.g. february) in dropdown list,gridview should display all table values for fabruary month only(e.g.rohan leuva,2/28/2013,full,casual),means record which has month=2(february).How to overcome this issue?i tried but i can only display all the values in gridview at this moment.Any help would be greatly appriciated.

SqlConnection conn = new SqlConnection();
conn.ConnectionString=System.Configuration.ConfigurationManager.ConnectionStrings["leave"].ConnectionString;
conn.Open();
SqlCommand cmd = new SqlCommand("select date1,leave,ltype from leave_rec where name='" + DropDownList1.SelectedValue + "'", conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();





上面的代码显示date1,leave,ltype for dropdownlist1.selectedvalue.but now我希望第二次下拉,其中几个月将在那里。所以当我在第二个选择fabruary时,网格应显示dropdownlist1.selectedvalue 的值仅适用于fabruary。

推荐答案

过滤数据表然后重新绑定gridview

例如:

DataView dv = ds.Tables [0] .DefaultView;



dv.RowFilter =date1 = Feb



filter data table with then rebind the gridview
e.g:
DataView dv=ds.Tables[0].DefaultView;

dv.RowFilter = "date1=Feb"

GridView1.DataSource = dv;
GridView1.DataBind();


这篇关于根据下拉列表值从数据库中选择值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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