views:

53

answers:

3

I have a GridView with a row number (Container.DataItemIndex) column in ASP.NET (1,2,3,4, ...)
I added it in GridView by following code :

<asp:TemplateField HeaderText="#" SortExpression="#">
            <ItemTemplate>
                <%# Container.DataItemIndex + 1 %>
            </ItemTemplate>
</asp:TemplateField>

I wanna sort the GridView by this column.
I mean
ASC : 1,2,3,4, ...
DESC : n,n-1, ... , 4,3,2,1

How can I do it ?

Edit:
I did it by this way http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sorting.aspx but it sorts # column like 1,11,12,13,...,2,21,22,...
I want to sort # column 1,2,3,...,11,12,13,...

A: 

(declaratively is sort of easier at first, but it's good to learn the programmatic way too)

JohnB
Well, I knew it, but when I sort # column it shows it like 1, 11, 12, 2, 21, 22, ... I don't want it, I wanna 1,2,3, ... , 11,12, ...
Mohammad
A: 

That sounds like it's sorting by string, does this work?

<ItemTemplate> 
    <%# Convert.ToInt32(Container.DataItemIndex + 1) %> 
</ItemTemplate>
Brissles
No, It doesn't work.
Mohammad
A: 

I've used the following methods ans my problem was solved :

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;
    }
}
Mohammad