4 Replies - 1647 Views - Last Post: 19 February 2010 - 10:16 AM Rate Topic: -----

#1 g_ervacio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-September 09

How to sort each row in a GridView based from a Field [RankOrder] from

Posted 08 February 2010 - 12:59 PM

I need your help but have ideas but do not know how to implement them.

What I want is that the order of how the groups will eventually appear in the gridview are read from the database.

I have 1 to x groups.

In my GridView, the data are displayed with BoundField.

It looks like this.

My question is how can sort each row?

Attached image(s)

  • Attached Image

Is This A Good Question/Topic? 0
  • +

Replies To: How to sort each row in a GridView based from a Field [RankOrder] from

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: How to sort each row in a GridView based from a Field [RankOrder] from

Posted 08 February 2010 - 02:26 PM

That depends.

Are you trying to sort the data before it is displayed to the client or do you want the client to have the ability to sort by a column of their choice?

If you want to sort it prior to displaying to the client, then modify your SQL query to ORDER BY whichever column you want to sort by.

If you want the client to be able pick and choose which column/direction to sort, then the GridView has sorting built into it already. You just need to implement it.

Gridview Sorting
Was This Post Helpful? 0
  • +
  • -

#3 g_ervacio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-September 09

Re: How to sort each row in a GridView based from a Field [RankOrder] from

Posted 08 February 2010 - 02:55 PM

Hi what im askin myself is how i can sort my gridview depending on the rank column in the database?
Its Loos like this


Posted Image


When one of the arrows is clicked on, for example Asceding then then the line should high slip. Let's say we click the down arrow for AEC, then its value must be recalculated, which would be called that previous rank_value [2] jumps to 1 And the value of 1 was is then 2 Once everything is to be bound to the GridView.


Posted Image

This post has been edited by g_ervacio: 09 February 2010 - 04:52 AM

Was This Post Helpful? 0
  • +
  • -

#4 Guest_Divya*


Reputation:

Re: How to sort each row in a GridView based from a Field [RankOrder] from

Posted 18 February 2010 - 06:21 AM

TRY this really

i think its userfull for u........

<-- code for ASP.NET-->

[b]<asp:GridView ID="gv1" runat="server" AutoGenerateColumns="False" AllowPaging="true" AllowSorting="true" DataKeyNames="EmployeeID" ShowFooter="True" RowStyle-Height="21px" [b]OnSorting="gv1_Sorting">[/b]
                   
                    <Columns>
                         <asp:TemplateField HeaderText="Rank order" HeaderStyle-CssClass="gridHeader" ControlStyle-Width="110px"
                            [b]SortExpression="Rank_order">[/b]
                            <ItemTemplate>
                                <span style="color: black">
                                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("Rank_order") %>'></asp:Label>
                                </span>
                            </ItemTemplate>
                        </asp:TemplateField>
   
                    </Columns>
                </asp:GridView>[/b]


//CODE FOR C#
 [b]protected void gv1_Sorting(object sender, GridViewSortEventArgs e)
        {
            SqlConnection conA;
            conA = new SqlConnection(stringconnection);
            SqlCommand cmdA = new SqlCommand("SP_Name", conA);
            cmdA.CommandType = CommandType.StoredProcedure;
            conA.Open();
            cmdA.Parameters.Add("@Pra_name", SqlDbType.VarChar).Value = TXT1.Text;
           
            SqlDataReader sdr = cmdA.ExecuteReader(Commandbehavior.CloseConnection);
            DataTable dts = new DataTable();
            dts.Load(sdr);
                if (dt != null)
                {
                    if (dt.Rows.Count > 0)
                    {

                        gv1.DefaultView.Sort = e.SortExpression + " " + [b]GetSortDirection[/b](e.SortExpression);

                        //GetSortDirection function given below 
                        gv1.DataSource = dt.DefaultView;
                        gv1.DataBind();
                    }
                }
            }
     private string [b]GetSortDirection[/b](string column)
     {

         // By default, set the sort direction to ascending.
         string sortDirection = "ASC";

         // Retrieve the last column that was sorted.
         string sortExpression = ViewState["SortExpression"] as string;

         if (sortExpression != null)
         {
             // Check if the same column is being sorted.
             // Otherwise, the default value can be returned.
             if (sortExpression == column)
             {
                 string lastDirection = ViewState["SortDirection"] as string;
                 if ((lastDirection != null) && (lastDirection == "ASC"))
                 {
                     sortDirection = "DESC";
                 }
             }
         }[/b]
         // Save new values in ViewState.
         ViewState["SortDirection"] = sortDirection;
         ViewState["SortExpression"] = column;

         return sortDirection;
     }


Was This Post Helpful? 0

#5 woodjom  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 29
  • View blog
  • Posts: 549
  • Joined: 08-May 08

Re: How to sort each row in a GridView based from a Field [RankOrder] from

Posted 19 February 2010 - 10:16 AM

how about simply setting that Column to SortExpression?

Its a column attribute but will cut down on your code for sorting. Unless its not a alphanumeric sort order and is more complicated than that.

Personally i do my sorting through my SQL queries and just the data view controls to display and manipulate the data.

There is a collection object that something similar to this. Why not load your data into an instance of that collection and operate off of the collection then when done update the database according to the collection.

This post has been edited by woodjom: 19 February 2010 - 10:23 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1