使用Dropdownlist和Gridview进行排序 [英] Sorting using Dropdownlist with Gridview
问题描述
您好FRNDS,请解决GRIDVIEW和DROPDOWNLIST所面临的问题.
我正在使用ASp.NET,C#,SqlSERVER2005.
在我的网页上,我有一个下拉列表和一个Gridview ...数据与数据库完全无关.这个gridview包含PAGING ALSO(它包含GRIDVIEW中的大量记录),这里的事情是我需要根据下拉列表对数据进行排序.
从下拉列表项中选择时,它应该对完整的列进行排序.
在下拉列表中,我将项目用作
1)按员工姓名排序---此字母必须按字母A-Z排序
2)按录用日期排序----升序
3)按终止日期排序---升序.
当我从下拉列表中选择一项时,该特定列必须在Gridview中排序.
请帮助我,并解决此问题.
谢谢.
Hello FRNDS, Please solve my problem facing with GRIDVIEW AND DROPDOWNLIST.
Am working on ASp.NET, c#, SqlSERVER2005.
On my webpage I have a Dropdownlist and a Gridview...data is dispalying perfectly from database. and this gridview contains PAGING ALSO(it contains Large number of records in GRIDVIEW) and the thing here is I need to sort the data based upon dropdown.
It should sort the complete column when selected from dropdownlist item.
In dropdownlist I used items as
1)sort by Employee name ---This must be sort alphabetwise A-Z
2)Sort by Hiring Date ---- Ascending order
3)Sort by Terminating Date --- Ascending Order.
When i select one item from Dropdownlist, that particular column must sort in Gridview.
Please help me, and solve this problem.
THANKS IN ADVANCE.
推荐答案
使用这个:
Hi,
Use this:
//Dropdownlist selected value changed event. This can be done on button click event also.
protected void DropDownList1_SelectIndexChanged(object sender, EventArgs e) {
gvSorting.Sort(DropDownList1.SelectedValue, SortDirection.Ascending);
}
//Gridview sort command
protected void gvSorting_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dtSortTable = gvSorting.DataSource as DataTable;
if (dtSortTable != null)
{
DataView dvSortedView = new DataView(dtSortTable);
dvSortedView.Sort = e.SortExpression + " " + getSortDirectionString(e.SortDirection);
gvSorting.DataSource = dvSortedView;
gvSorting.DataBind();
}
}
//Getting the sort direction
private static string getSortDirectionString(SortDirection sortDireciton)
{
string newSortDirection = String.Empty;
if (sortDireciton == SortDirection.Ascending)
{
newSortDirection = "ASC";
}
else
{
newSortDirection = "DESC";
}
return newSortDirection;
}
祝一切顺利.
--Amit
All the best.
--Amit
这篇关于使用Dropdownlist和Gridview进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!