0
排序有這個gridview的其中有一個需要排序的兩列。允許基於列的gridview的
的問題是我收到類的列表中的數據綁定到GridView的數據源,在格欄點擊排序表達式火災,但數據源是空的,所以我怎麼能做到這一點。
標記:
<asp:GridView ID="grdDelegateList" runat="server" CssClass="gridviewBorder" AutoGenerateColumns="False" AllowSorting="true"
EmptyDataText="There are no delegates assigned for this bridge." CellPadding="2"
Style="margin-left: 0px" BackColor="White" Font-Names="Calibri" BorderWidth="1px"
Width="100%" AllowPaging="True" GridLines="Horizontal" RowHoverBackColor="#666666"
RowHoverForeColor="White" SelectedRowStyle-BackColor="#333333" SelectedRowStyle-ForeColor="White"
PageSize="20" OnPageIndexChanging="grdDelegateList_PageIndexChanging" OnRowCommand="grdDelegateList_RowCommand"
OnRowDataBound="grdDelegateList_RowDataBound"
OnRowDeleting="grdDelegateList_RowDeleting" onsorting="grdDelegateList_Sorting">
<Columns>
<asp:BoundField HeaderText="Employee ID" DataField="DelegateID" ItemStyle-HorizontalAlign="Center" SortExpression="DelegateID"
HeaderStyle-HorizontalAlign="Center" />
<asp:BoundField HeaderText="Display Name" DataField="FullName" ItemStyle-HorizontalAlign="Left" SortExpression="FullName"
HeaderStyle-HorizontalAlign="Left" />
<asp:TemplateField HeaderText="Remove" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<span style="cursor: pointer">
<asp:LinkButton ID="ImgRemove" runat="server" CommandName="Delete" CommandArgument='<%# Eval("ID") %>'
Text="Remove" OnClientClick="return confirm('Are you sure you want to remove this Delegate');">
<img alt="Remove" src="Images/trash.png" style="border:0;" />
</asp:LinkButton></span>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
排序表達式事件:
protected void grdDelegateList_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable table = grdDelegateList.DataSource as DataTable;//this is coming null
if (table != null)
{
DataView dataView = new DataView(table);
dataView.Sort = e.SortExpression + " " + ConvertSortDirection(e.SortDirection);
grdDelegateList.DataSource = dataView;
grdDelegateList.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;
}
由於數據源爲空,我需要從分貝再次,如果是的,我取的列表獲取數據,所以我需要添加cutom邏輯來首先將列表轉換爲數據表或將它與列表一起工作。有什麼建議麼??
該類的列表中有一個其他類的對象,用於獲取該類的信息.so datatable將不起作用... – ankur
然後,您應該使用將數據源強制轉換爲列表而不是Datatable並執行對列表項進行排序。 – vendettamit
我已經在代碼中的變化,現在它是獲取數據表,但在分揀表達事件的griddatasource仍然是空.. – ankur