<asp:GridView AlternatingRowStyle-BackColor="#E2E2E2" AutoGenerateColumns="false" OnSorting="yourTasksGV_Sorting" AllowSorting="true" ID="yourTasksGV" runat="server" ClientIDMode="Static" EmptyDataText="There is no data to display" OnRowDataBound="yourTasksGV_RowDataBound">
<Columns>
<asp:HyperLinkField Target="_blank" DataNavigateUrlFields="Task Detail" DataTextField="Task Name" DataNavigateUrlFormatString="" HeaderText="Task Details" SortExpression="TaskDetails" ItemStyle-Width="35%" ItemStyle-CssClass="taskTableColumn" />
<asp:BoundField DataField="Services" HeaderText="Services" SortExpression="ServiceName" ItemStyle-Width="25%" ItemStyle-CssClass="taskTableColumn" />
<asp:BoundField DataField="Status" HeaderText="Status" SortExpression="TheStatus" ItemStyle-Width="15%" ItemStyle-CssClass="taskTableColumn" />
<asp:BoundField DataField="Due Date" HeaderText="Due Date" SortExpression="DueDate" ItemStyle-Width="15%" ItemStyle-CssClass="taskTableColumn" />
</Columns>
</asp:GridView>
代码隐藏:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
PullData();
}
}
public void PullData()
{
DataTable taskData = new DataTable();
string connString = @"user id = user;" + "password= pss$$$$; server= dev-magician; database= OnBase;" /*+ "Trusted_Connection=yes;"*/ + "connection timeout=30";
string query = @"SELECT 'http://mg/appnet/workview/objectPop.aspx?objectid=' + CAST(CT.OBJECTID AS VARCHAR) + '&classid=1224' 'Task Detail'
,UG.USERGROUPNAME 'Services'
,CT.ATTR2812 'Status'
,CT.ATTR2752 'Due Date'
,CT.ATTR2739 'Task Name'
FROM HSI.RMOBJECTINSTANCE1224 CT LEFT JOIN HSI.USERGROUP UG on CT.FK2743 = UG.USERGROUPNUM
WHERE CT.ACTIVESTATUS = 0";
using (SqlConnection conn = new SqlConnection(connString))
{
try
{
SqlCommand cmd = new SqlCommand(query, conn);
// create data adapter
SqlDataAdapter da = new SqlDataAdapter(query, conn);
// this will query your database and return the result to your datatable
da.Fill(taskData);
//conn.Close();
yourTasksGV.DataSource = taskData;
yourTasksGV.DataBind();
Session["sortData"] = taskData;
}
catch (Exception ex)
{
string error = ex.Message;
}
}
}
protected void yourTasksGV_Sorting(object server, GridViewSortEventArgs e)
{
/*string strSortExpression = e.SortExpression;
switch (strSortExpression)
{
case "TaskDetails":
MessageBox.Show("Task Details");
break;
}*/
DataTable dataTable = Session["sortData"] as DataTable;
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
dataView.Sort = e.SortExpression + " " + ConvertSortDirection(e.SortDirection);
yourTasksGV.DataSource = dataView;
yourTasksGV.DataBind();
}
}
private string ConvertSortDirection(SortDirection sortDirection)
{
string newSortDirection = String.Empty;
switch (sortDirection)
{
case SortDirection.Ascending:
newSortDirection = "ASC";
break;
case SortDirection.Descending:
newSortDirection = "DESC";
break;
}
return newSortDirection;
}
我想要实现的是每当我点击任何标题时它应该对GridView进行排序,但无论我点击哪个标题文本进行排序,我都会收到以下错误:
Exception Details: System.IndexOutOfRangeException: Cannot find column ServiceName.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[IndexOutOfRangeException: Cannot find column ServiceName.]
System.Data.DataTable.ParseSortString(String sortString) +5313286
System.Data.DataView.CheckSort(String sort) +35
System.Data.DataView.set_Sort(String value) +115
TestOnBase.Pages.Default.yourTasksGV_Sorting(Object server, GridViewSortEventArgs e) +256
System.Web.UI.WebControls.GridView.OnSorting(GridViewSortEventArgs e) +122
System.Web.UI.WebControls.GridView.HandleSort(String sortExpression, SortDirection sortDirection) +65
System.Web.UI.WebControls.GridView.HandleSort(String sortExpression) +78
System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +497
System.Web.UI.WebControls.GridView.RaisePostBackEvent(String eventArgument) +205
System.Web.UI.WebControls.GridView.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +9528578
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724
--------------------------------------------------------------------------------
如何修改代码以便用户点击它的任何标题文本,它会进行排序,然后在另一次单击时对其进行排序等等?
数据表:
如何向用户显示:
答案 0 :(得分:2)
您要对表达式ServiceName
进行排序,但您的列名为Services
。
<asp:BoundField DataField="Services"
HeaderText="Services"
SortExpression="Services"
ItemStyle-Width="25%"
ItemStyle-CssClass="taskTableColumn" />
有关详细说明,SortExpression
用于对绑定到控件的数据源进行排序。因此,与SQL ORDER BY
语句一样,您可以指定要对其进行排序的列。
因此,在您的查询中,您将返回五列:任务详细信息,服务,状态,截止日期和任务名称。将“服务”列绑定到标签时,也就是必须排序的列,因为基础数据源具有“服务”列。因此,即使GridView的列标题完全不同,您仍需要按数据源的列名称对其进行排序。