我在ASP.NET中有一个带有行号(Container.DataItemIndex)列的GridView(1,2,3,4,...)
我通过以下代码在GridView中添加了它:
<asp:TemplateField HeaderText="#" SortExpression="#">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
我想通过这一栏对GridView进行排序 我的意思是
ASC : 1,2,3,4, ...
DESC : n,n-1, ... , 4,3,2,1
我该怎么做?
编辑:
我是通过这种方式http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sorting.aspx完成的,但它将#column排序为1,11,12,13,...,2,21,22,...
我想对#column 1,2,3,...,11,12,13,...
答案 0 :(得分:0)
(一开始声明性更容易,但是学习程序化方式也很好)
答案 1 :(得分:0)
这听起来像是按字符串排序,这有用吗?
<ItemTemplate>
<%# Convert.ToInt32(Container.DataItemIndex + 1) %>
</ItemTemplate>
答案 2 :(得分:0)
我使用了以下方法,我的问题解决了:
HTML:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" AllowSorting="true"
EmptyDataText="داده ای برای نمایش وجود ندارد" Width="100%" OnPageIndexChanging="GridView1_PageIndexChanging"
OnSelectedIndexChanging="GridView1_SelectedIndexChanging" CssClass="GridViewStyle"
OnSorting="GridView1_Sorting">
<FooterStyle CssClass="GridViewFooterStyle" />
<RowStyle CssClass="GridViewRowStyle" />
<SelectedRowStyle CssClass="GridViewSelectedRowStyle" />
<PagerStyle CssClass="GridViewPagerStyle" />
<AlternatingRowStyle CssClass="GridViewAlternatingRowStyle" />
<HeaderStyle CssClass="GridViewHeaderStyle" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton runat="server" ID="btnDelete" CommandName="cDelete" ImageUrl="~/Pages/images/DeleteIcon.png"
OnCommand="OnDelete" CommandArgument='<%# Bind("GridViewDefinitionID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField SelectText="ویرایش" ShowSelectButton="True" ButtonType="Image"
SelectImageUrl="~/Pages/images/EditIcon.gif" EditImageUrl="~/Pages/images/EditIcon.gif">
</asp:CommandField>
<asp:TemplateField HeaderText="TransactionKey" SortExpression="TransactionKey">
<EditItemTemplate>
<asp:TextBox ID="TextBoxGridViewTransactionKey" runat="server" Text='<%# Bind("GridViewTransactionKey") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LabelGridViewTransactionKey" runat="server" Text='<%# Bind("GridViewTransactionKey") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TerminalID" SortExpression="TerminalID">
<EditItemTemplate>
<asp:TextBox ID="TextBoxGridViewTerminalID" runat="server" Text='<%# Bind("GridViewTerminalID") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LabelGridViewTerminalID" runat="server" Text='<%# Bind("GridViewTerminalID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="MerchantID" SortExpression="MerchantID">
<EditItemTemplate>
<asp:TextBox ID="TextBoxGridViewMerchantID" runat="server" Text='<%# Bind("GridViewMerchantID") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LabelGridViewMerchantID" runat="server" Text='<%# Bind("GridViewMerchantID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="شماره حساب" SortExpression="شماره حساب">
<EditItemTemplate>
<asp:TextBox ID="TextBoxGridViewBankAccount" runat="server" Text='<%# Bind("GridViewBankAccount") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LabelGridViewBankAccount" runat="server" Text='<%# Bind("GridViewBankAccount") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="شعبه" SortExpression="شعبه">
<ItemTemplate>
<asp:Label ID="LabelGridViewBankBranch" runat="server" Text='<%# Bind("GridViewBankBranch") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="نام بانک" SortExpression="نام بانک">
<EditItemTemplate>
<asp:TextBox ID="TextBoxGridViewBankName" runat="server" Text='<%# Bind("GridViewBankName") %>'
Style="direction: rtl;"></asp:TextBox>
<asp:HiddenField ID="HiddenFieldID" runat="server" Value='<%# Bind("GridViewDefinitionID") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LabelGridViewBankName" runat="server" Text='<%# Bind("GridViewBankName") %>'></asp:Label>
<asp:HiddenField ID="HiddenFieldID" runat="server" Value='<%# Bind("GridViewDefinitionID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="#" SortExpression="#">
<ItemTemplate>
<asp:Label ID="LabelGridViewRowNumber" runat="server" Text='<%# Bind("GridViewRowNumber") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
C#:
DataView SortGridView()
{
BankTransactionsDataSet1 dataSet = new BankTransactionsDataSet1();
BankTransactionsDataClassesDataContext dbc = new BankTransactionsDataClassesDataContext();
var Definitions = dbc.Definitions;
if (Definitions.Count() <= 0) return null;
int i = 0;
foreach (var Definition in Definitions)
{
i++;
string bankName = dbc.Banks.Where(c => c.BankID == Definition.BankID).First().BankName;
string bankBranch = dbc.Banks.Where(c => c.BankID == Definition.BankID).First().BankBranch;
dataSet.DataTableBanks.Rows.Add(bankName, Definition.BankAccount,
Definition.MerchantID, Definition.TerminalID,
Definition.TransactionKey, Definition.DefinitionID,
i.ToString(), bankBranch);
}
string SortExpression = (ViewState["SortExpression"] as string) == null ? "GridViewBankName" : ViewState["SortExpression"] as string;
string lastDirection = (ViewState["SortDirection"] as string) == null ? "ASC" : ViewState["SortDirection"] as string;
DataView newBankDataTable = new DataView();
switch (SortExpression)
{
case "GridViewTransactionKey":
if (lastDirection == "ASC")
newBankDataTable = dataSet.DataTableBanks.OrderBy(q => q.GridViewTransactionKey).AsDataView();
else
newBankDataTable = dataSet.DataTableBanks.OrderByDescending(q => q.GridViewTransactionKey).AsDataView();
break;
case "GridViewTerminalID":
if (lastDirection == "ASC")
newBankDataTable = dataSet.DataTableBanks.OrderBy(q => q.GridViewTerminalID).AsDataView();
else
newBankDataTable = dataSet.DataTableBanks.OrderByDescending(q => q.GridViewTerminalID).AsDataView();
break;
case "GridViewMerchantID":
if (lastDirection == "ASC")
newBankDataTable = dataSet.DataTableBanks.OrderBy(q => q.GridViewMerchantID).AsDataView();
else
newBankDataTable = dataSet.DataTableBanks.OrderByDescending(q => q.GridViewMerchantID).AsDataView();
break;
case "GridViewBankAccount":
if (lastDirection == "ASC")
newBankDataTable = dataSet.DataTableBanks.OrderBy(q => q.GridViewBankAccount).AsDataView();
else
newBankDataTable = dataSet.DataTableBanks.OrderByDescending(q => q.GridViewBankAccount).AsDataView();
break;
case "GridViewBankName":
if (lastDirection == "ASC")
newBankDataTable = dataSet.DataTableBanks.OrderBy(q => q.GridViewBankName).AsDataView();
else
newBankDataTable = dataSet.DataTableBanks.OrderByDescending(q => q.GridViewBankName).AsDataView();
break;
case "GridViewRowNumber":
if (lastDirection == "ASC")
newBankDataTable = dataSet.DataTableBanks.OrderBy(q => int.Parse(q.GridViewRowNumber)).AsDataView();
else
newBankDataTable = dataSet.DataTableBanks.OrderByDescending(q => int.Parse(q.GridViewRowNumber)).AsDataView();
break;
default:
dataSet.DataTableBanks.DefaultView.Sort = SortExpression + " " + lastDirection;
break;
}
return newBankDataTable;
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
try
{
switch (e.SortExpression)
{
case "TransactionKey":
ViewState["SortExpression"] = "GridViewTransactionKey";
break;
case "TerminalID":
ViewState["SortExpression"] = "GridViewTerminalID";
break;
case "MerchantID":
ViewState["SortExpression"] = "GridViewMerchantID";
break;
case "شماره حساب":
ViewState["SortExpression"] = "GridViewBankAccount";
break;
case "نام بانک":
ViewState["SortExpression"] = "GridViewBankName";
break;
case "#":
ViewState["SortExpression"] = "GridViewRowNumber";
break;
default:
break;
}
string lastDirection = ViewState["SortDirection"] as string;
string sortDirection = "DESC";
if ((lastDirection != null) && (lastDirection == "DESC")) sortDirection = "ASC";
ViewState["SortDirection"] = sortDirection;
GridView1.DataSource = SortGridView();
GridView1.DataBind();
}
catch (Exception ex)
{
LabelResult.Text = ex.Message;
}
}