in

C1 Community

ComponentOne Community is a free source for developers and help authors to collaborate and communicate.

Grid Only Sorts One Way

Last post 12-12-2007 1:19 PM by orandov. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 12-10-2007 1:41 PM

    Grid Only Sorts One Way

    Hi,

    I am using VS 2005 and webgrid 2.1.20071.97.

    I am trying to implement Ajax enabled sorting and when I click on the column to fire the sorting command the sort direction is always ascending. This happens even after I changed the sort expression on the datasource to descending. What am I doing wrong?

    Thank you,

    Oran Levin

    Here is my code:

    Protected Sub grdMessages_SortingCommand(ByVal sender As System.Object, ByVal e As C1.Web.C1WebGrid.C1SortingCommandEventArgs) Handles grdMessages.SortingCommand

    dvMessages = CType(Session("dvMessages"), DataView)

    If Not dvMessages Is Nothing Then

    If e.Column.SortDirection = C1.Web.C1WebGrid.C1SortDirection.Ascending Then  '''This statement is always true. WHY?

    dvMessages.Sort = e.SortExpression + " desc"

    Else

    dvMessages.Sort = e.SortExpression + " asc"

    End If

    SetGridSource(dvMessages)

    End If

    End Sub

    Public Sub SetGridSource(ByVal dvMessages As DataView) 'osqlmessagebr As SQLMessageBR)

    grdMessages.DataSource = dvMessages

    grdMessages.DataBind()

    grdMessages.Columns(10).Visible = False 'Hide stack trace

    grdMessages.Columns(11).Visible = False 'Hide picture

    grdMessages.Columns(1).HeaderText = "ID"

    Session.Add("dvMessages", dvMessages)

    End Sub

    Filed under:
  • 12-12-2007 6:35 AM In reply to

    Re: Grid Only Sorts One Way

    Please refer to code below to sort the grid:
     
    C1WebGrid grid = new C1WebGrid();

    protected void Page_Load(object sender, EventArgs e)

    {

    form1.Controls.Add(grid);

    Fill();

    }

    //Added*

    DataView dv;

    DataTable CreateDS()

    {

    if (Session["DataSource"] == null)

    {

    string path = Server.MapPath(".");

    OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + @"\wpdata.mdb;Persist Security Info=False");

    con.Open();

    OleDbCommand com = new OleDbCommand();

    com.CommandText = "select *from da_data";

    com.Connection = con;

    OleDbDataAdapter ad = new OleDbDataAdapter();

    ad.SelectCommand = com;

    DataSet ds = new DataSet();

    ad.Fill(ds);

    DataTable dt = new DataTable();

    dt = ds.Tables[0];

    Session.Add("DataSource", dt);

    }

    return ((DataTable)(Session["DataSource"]));

    }

    private void Fill()

    {

    grid.Columns.Clear();

    C1ButtonColumn dcc1 = new C1ButtonColumn();

    //dcc1.Fixed = true;

    dcc1.ButtonType = ButtonColumnType.LinkButton;

    dcc1.DataTextField = "Descritpion";

    dcc1.HeaderText = "DIVISION";

    dcc1.SortExpression = "Descritpion";

    dcc1.HeaderStyle.Font.Bold = true;

    C1BoundColumn col2 = new C1BoundColumn();

    col2.DataField = "Budget";

    col2.HeaderText = "BUDGET";

    col2.SortExpression = "Budget";

    col2.DataFormatString = "{0:###,###,##0}";

    col2.ItemStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Right;

    col2.FooterText = string.Format("{0:c0}", double.Parse("12345678"));

    col2.FooterStyle.Font.Bold = true;

    col2.FooterStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Right;

    C1BoundColumn col3 = new C1BoundColumn();

    col3.DataField = "Billable";

    col3.HeaderText = "Billable";

    col3.SortExpression = "Billable";

    col3.DataFormatString = "{0:###,###,##0}";

    col3.ItemStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Right;

    col3.FooterText = string.Format("{0:c0}", double.Parse("3412345"));

    col3.FooterStyle.Font.Bold = true;

    col3.FooterStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Right;

    C1BoundColumn col4 = new C1BoundColumn();

    col4.DataField = "Profitindex";

    col4.HeaderText = "Profitindex";

    col4.SortExpression = "Profitindex";

    col4.DataFormatString = "{0:###,###,##0.00}";

    col4.ItemStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Right;

    col4.FooterText = "total1";

    col4.FooterText = string.Format("{0:c0}", double.Parse("78250"));

    col4.FooterStyle.Font.Bold = true;

    col4.FooterStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Right;

    grid.ShowFooter = true;

    grid.CallbackOptions = CallbackOptionsEnum.Sorting;

    grid.AllowSorting = true;

    grid.SortingCommand += new C1SortingCommandEventHandler(grid_SortingCommand);

    grid.ItemCommand += new C1CommandEventHandler(grid_ItemCommand);

    grid.AutoGenerateColumns = false;

    grid.DataKeyField = "Descritpion";

    grid.Columns.Add(dcc1);

    grid.Columns.Add(col2);

    grid.Columns.Add(col3);

    grid.Columns.Add(col4);

    DataTable dt = (DataTable)CreateDS();

    dv = dt.DefaultView;

    grid.DataSource = dv;

    grid.DataBind();

    }

    void grid_ItemCommand(object sender, C1CommandEventArgs e)

    {

    Response.Write("Grid Key Value :" + grid.DataKeys[e.Item.ItemIndex].ToString());

    }

    void grid_SortingCommand(object sender, C1SortingCommandEventArgs e)

    {

    if ((dv != null))

    {

    if (e.Column.SortDirection == C1.Web.C1WebGrid.C1SortDirection.Ascending)

    {

    dv.Sort = e.SortExpression + " desc";

    }

    else

    {

    dv.Sort = e.SortExpression + " asc";

    }

    grid.DataSource = dv;

    grid.DataBind();

    }

    }

    Regards,

    Patrick

    <orandov> wrote in message news:200235@10.0.1.98...

    Hi,

    I am using VS 2005 and webgrid 2.1.20071.97.

    I am trying to implement Ajax enabled sorting and when I click on the column to fire the sorting command the sort direction is always ascending. This happens even after I changed the sort expression on the datasource to descending. What am I doing wrong?

    Thank you,

    Oran Levin

    Here is my code:

    Protected Sub grdMessages_SortingCommand(ByVal sender As System.Object, ByVal e As C1.Web.C1WebGrid.C1SortingCommandEventArgs) Handles grdMessages.SortingCommand

    dvMessages = CType(Session("dvMessages"), DataView)

    If Not dvMessages Is Nothing Then

    If e.Column.SortDirection = C1.Web.C1WebGrid.C1SortDirection.Ascending Then  '''This statement is always true. WHY?

    dvMessages.Sort = e.SortExpression + " desc"

    Else

    dvMessages.Sort = e.SortExpression + " asc"

    End If

    SetGridSource(dvMessages)

    End If

    End Sub

    Public Sub SetGridSource(ByVal dvMessages As DataView) 'osqlmessagebr As SQLMessageBR)

    grdMessages.DataSource = dvMessages

    grdMessages.DataBind()

    grdMessages.Columns(10).Visible = False 'Hide stack trace

    grdMessages.Columns(11).Visible = False 'Hide picture

    grdMessages.Columns(1).HeaderText = "ID"

    Session.Add("dvMessages", dvMessages)

    End Sub



    http://helpcentral.componentone.com/cs/forums/p/73759/200235.aspx#200235

  • 12-12-2007 1:19 PM In reply to

    Re: Grid Only Sorts One Way

    Thank you for the reply.

     I was auto generating my columns in the grid based on the datasource. Your example had databound columns created. So I changed my grid to have databound columns at design time and the sorting worked.

    Is that the only way for the sort to work (with databound columns already created)?

Page 1 of 1 (3 items)
Contact ComponentOne: 1.800.858.2739 ©1987-2008 ComponentOne LLC All Rights Reserved.