locked
Autosorting a GridView RRS feed

  • Question

  • User253847291 posted

    ive  the following control on my page:

    <asp:GridView ID="gvMail" runat="server" BackColor="#E5F2E5" Width="500px" AutoGenerateColumns="false" AllowPaging="True" AllowSorting="True" CellPadding="2" Caption="<a style='font-weight:bold;color:#008000;'>Bound e-mail and domains</a>" Font-Names="verdana" Font-Size="8pt" OnPageIndexChanged="gvMail_PageIndexChanged" OnPageIndexChanging="gvMail_PageIndexChanging" OnSelectedIndexChanged="gvMail_SelectedIndexChanged" OnSorting="gvMail_Sorting" PageSize="8">

    <PagerSettings Position="Top" />
    <Columns>
    <asp:BoundField HeaderText="e-mail" ReadOnly="True" SortExpression="mail" DataField="mail" />
    <asp:BoundField HeaderText="type" ReadOnly="True" SortExpression="status" />
    <asp:BoundField HeaderText="status" ReadOnly="True" SortExpression="status" DataField="status" />
    <asp:HyperLinkField InsertVisible="False" ShowHeader="False" Text="delete" NavigateUrl="?id=mytestid" />
    </Columns>

    <PagerStyle HorizontalAlign="Right" />
    <HeaderStyle Font-Bold="False" HorizontalAlign="Center" />
    </asp:GridView>

    when i try to sort using the e-mail field it does not work

    the code behind set the data source while !IsPostBack only:

    gvMail.DataSource = engine.BoundMail(user_id);
    gvMail.DataBind();

    the engine class provide a DataTable to fill the read only grid.

    i wish that when i click on the column header the grid is sorted accordingly, switching asc/des automatically

    Wednesday, December 31, 2008 2:03 AM

Answers

  • User-481631678 posted

    try this code

     protected void gvMail_Sorting(object sender, GridViewSortEventArgs e)
        {

            DataTable dt = engine.BoundMail(user_id);
            DataView dv = new DataView(dt);
            dv.Sort = e.SortExpression;

    gvMail.DataSource = dv;
    gvMail.DataBind();

        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 31, 2008 3:37 AM

All replies

  • User-481631678 posted

    try this code

     protected void gvMail_Sorting(object sender, GridViewSortEventArgs e)
        {

            DataTable dt = engine.BoundMail(user_id);
            DataView dv = new DataView(dt);
            dv.Sort = e.SortExpression;

    gvMail.DataSource = dv;
    gvMail.DataBind();

        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 31, 2008 3:37 AM
  • User-481631678 posted

     Did it resolved your problem

    Thursday, January 1, 2009 12:34 AM
  • User-260435932 posted

    Read http://forums.asp.net/t/956540.aspx

    Thursday, January 1, 2009 1:28 AM
  • User253847291 posted

    hello, is it possible to sort the grid elements without binding it again? for example i may want to only sort the records without refresh its recordset everytime.

     

    ty,

    Thursday, January 1, 2009 3:54 PM
  • User253847291 posted

    i got the reply to my last post from the link above.

    however sorting using this method generates an error, this is what im using, for now i keepts the binding, ill need to do the same thing on another grid without binding it everytime, it works for this one:

    protected void gvMail_Sorting(object sender, GridViewSortEventArgs e)
    {
    DataTable dt = engine.BoundMail(user_id);
    DataView dv = new DataView(dt);

    dv.Sort = e.SortExpression;
    gvMail.DataSource = dv;
    gvMail.DataBind();
    }

    here when i sort using this method it generate a null reference error at this step:

    protected void gvMail_RowCreated(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow) {
       DataRowView data = (DataRowView)e.Row.DataItem;
       if (data.Row.ItemArray[2].ToString() == "130 {
         e.Row.Cells[2].Text =
    "Pending";
         e.Row.BackColor = System.Drawing.
    Color.FromArgb(242, 242, 218);
       }
    else {
         e.Row.Cells[2].Text =
    "Verified";
      }
    }
    }

    why when ibind it again this event is not correctly fired as the first time?

    The error refer that data is null.


    ty,

    Thursday, January 1, 2009 4:06 PM
  • User253847291 posted

    was using the wrong event, switching to RowDataBound solved the problem.

    Thursday, January 1, 2009 4:25 PM