主题:DataView的一个问题
下面的一段代码是实现选中DropDownList中的在ListBox中显示,我的怎么实现不了,大家帮忙看看。谢谢
using System;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
private string myConnectionString = ConfigurationManager.ConnectionStrings["sqlconnectionstring"].ConnectionString;
private static DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//TestDataAdapter();
InitFilterList();
ds = GetDataSet();
}
}
private void InitFilterList()
{
FilterList.Items.Clear();
char aChar = 'a';
FilterList.Items.Add(new ListItem("all","-1"));
for (int i = 0; i < 26; i++)
{
FilterList.Items.Add(new ListItem(((char)((int)aChar+i)).ToString(),i.ToString()));//FilterList DropdownList;UserList1 ListBox
}
FilterList.SelectedIndex = 0;
}
private DataSet GetDataSet()
{
SqlConnection myConnection = new SqlConnection(myConnectionString);
string strSql = "select * from employees";
SqlDataAdapter myDa = new SqlDataAdapter(strSql,myConnection);
DataSet ds = new DataSet("UserDataSet");
try
{
myConnection.Open();
myDa.Fill(ds, "employees");
UserList1.DataSource = ds.Tables[0].DefaultView;
UserList1.DataTextField = "lastname";
UserList1.DataValueField = "employeeid";
UserList1.DataBind();
}
catch (SqlException ex)
{
Response.Write(ex.Message + "<br>");
}
finally
{
myConnection.Close();
}
return ds;
}
protected void FilterList_SelectedIndexChanged(object sender, EventArgs e)
{
DataView dv = ds.Tables[0].DefaultView;
if (FilterList.SelectedItem.Text == "a")
{
dv.RowFilter = "lastname like '*'";
//dv = ds.Tables[0].DefaultView;
}
else
{
dv.RowFilter = "lastname like '" + this.FilterList.SelectedItem.Text + "'";
//dv = ds.Tables[0].DefaultView;
}
UserList1.DataSource = dv;
UserList1.DataTextField = "lastname";
UserList1.DataValueField = "employeeid";
UserList1.DataBind();
}
using System;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
private string myConnectionString = ConfigurationManager.ConnectionStrings["sqlconnectionstring"].ConnectionString;
private static DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//TestDataAdapter();
InitFilterList();
ds = GetDataSet();
}
}
private void InitFilterList()
{
FilterList.Items.Clear();
char aChar = 'a';
FilterList.Items.Add(new ListItem("all","-1"));
for (int i = 0; i < 26; i++)
{
FilterList.Items.Add(new ListItem(((char)((int)aChar+i)).ToString(),i.ToString()));//FilterList DropdownList;UserList1 ListBox
}
FilterList.SelectedIndex = 0;
}
private DataSet GetDataSet()
{
SqlConnection myConnection = new SqlConnection(myConnectionString);
string strSql = "select * from employees";
SqlDataAdapter myDa = new SqlDataAdapter(strSql,myConnection);
DataSet ds = new DataSet("UserDataSet");
try
{
myConnection.Open();
myDa.Fill(ds, "employees");
UserList1.DataSource = ds.Tables[0].DefaultView;
UserList1.DataTextField = "lastname";
UserList1.DataValueField = "employeeid";
UserList1.DataBind();
}
catch (SqlException ex)
{
Response.Write(ex.Message + "<br>");
}
finally
{
myConnection.Close();
}
return ds;
}
protected void FilterList_SelectedIndexChanged(object sender, EventArgs e)
{
DataView dv = ds.Tables[0].DefaultView;
if (FilterList.SelectedItem.Text == "a")
{
dv.RowFilter = "lastname like '*'";
//dv = ds.Tables[0].DefaultView;
}
else
{
dv.RowFilter = "lastname like '" + this.FilterList.SelectedItem.Text + "'";
//dv = ds.Tables[0].DefaultView;
}
UserList1.DataSource = dv;
UserList1.DataTextField = "lastname";
UserList1.DataValueField = "employeeid";
UserList1.DataBind();
}