[英]Filter in a Dataset in asp.net C#
我想在搜索文本框中的textchange上過濾gridview。...如果我按“ S”之類的字符,則gridview應該填充以“ S”開頭的記錄。
Public class DALDepartment
{
public Dataset DepartmentSearch(string Connectionstring, string conditon)
{
SqlConnection connection = new SqlConnection(connectionstring);
SqlCommand command = new SqlCommand("select departmentname,departmentcode from Department" + condition, connection);
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet ds = new DataSet();
try
{
connection.Open(); //Opening Connection
adapter.Fill(ds, "Department"); //Filling values to Dataset from Adapter
connection.Close(); //closing connection
}
catch (Exception e)
{
ErrorCollection.AddErrors("XMS0000", e.Data + e.Message);
return null;
}
return ds;
}
}
//Dep.Aspx.cs in the asp page
DataSet ds = new DataSet();
string condition = "where departmentname LIKE '%" + Textbox1.Text + "%'" ;
ds=DepartmentSearch(Connectionstring,condition);
GridView1.DataSource = ds.Tables["Department"];
GridView1.DataBind();
以上代碼可以正常工作。 現在我需要在數據集中對此進行排序。 當頁面加載時,我將其存儲在數據集中。 例如:
//The Data's are found and loaded in a dataset
SqlCommand command = new SqlCommand("select departmentname,departmentcode from
Department" , connection);
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet ds = new DataSet();
connection.Open();
adapter.Fill(ds, "Department");
connection.Close();
//In dataset
DataSet ds = new DataSet();
ds = DepartmentSearch(Connectionstring);
現在,我不知道如何過濾數據集中的值。
在此先感謝您的朋友們的回答和建議。
您可以在DataTable的情況下使用Select函數,即
dtNames.Select(“名稱類似'a%'”);
因此,您將對名稱進行排序。請記住,名稱必須是數據表的一列。因此,下一次您只能通過更改字母進行搜索而無需執行查詢。但是此方法將僅檢索僅在以下情況下可用的數據執行第一個查詢。如果對數據庫進行了任何更改,則僅在執行查詢后才會反映出來。
如果您試圖從數據庫中選擇某些行並對其進行排序,則需要像下面這樣更改select命令:
SqlCommand command = new SqlCommand("select departmentname, departmentcode from Department " + condition + "order by departmentname", connection);
您想要的只是對數據集中的數據進行排序。 我不得不說,以任何其他方式這都是不可能的。
您可以使用BindingSource做您想做的事情。
BindingSource bs = new BindingSource();
bs.DataSource = ds;
bs.Sort = "departmentname asc";
bs.Filter = "departmentname like 'depa%'";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.