if e.Row.DataItem("important") = true .... add to the top of gridview

I have a gridview page that currently orders the page by date but it e.Row.DataItem("important") = true then i want to add it to the top of the page... can this be done??
runnerjp2005Asked:
Who is Participating?
 
Mohamed AbowardaSoftware EngineerCommented:
SELECT * FROM TableName ORDER by important DESC, date DESC

Open in new window


Change "date" to your date field name.
0
 
Imran Javed ZiaConsultant Software Engineer - .NET ArchitectCommented:
Yes,  you can use multi column sort by manually handling sort expresion
and use following expresion
important, dateField
0
 
runnerjp2005Author Commented:
how would i apply it?

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
                  AutoGenerateColumns="False" CellPadding="4" DataKeyNames="ID" 
                  DataSourceID="AccessDataSource1" ForeColor="#333333" GridLines="None" 
                  Width="970px">
                  <AlternatingRowStyle BackColor="#f9f9f9" ForeColor="Black" />
                  <Columns>
                      <asp:TemplateField>
                      <ItemTemplate>   
 <asp:Image ID="key" runat="server" />
    </ItemTemplate>   
                      </asp:TemplateField>
                      <asp:BoundField DataField="Title" HeaderText="Topic" SortExpression="Title" />
                      <asp:BoundField DataField="Auther" HeaderText="Topic Starter" 
                          SortExpression="Auther" />
                   <asp:TemplateField HeaderText="Replies" >   
<ItemTemplate>   
  <asp:Label ID="replies" runat="server" />
    </ItemTemplate>   
</asp:TemplateField>

                  <asp:TemplateField HeaderText="Last Post" >   
<ItemTemplate>   
  <asp:Label ID="lastrep" runat="server" />
    </ItemTemplate>   
</asp:TemplateField>

 <asp:BoundField DataField="views" HeaderText="Views" SortExpression="views" />

                  </Columns>
                  <EditRowStyle BackColor="#999999" />
                  <FooterStyle BackColor="#001C2A" ForeColor="White" Font-Bold="True" />
                  <HeaderStyle BackColor="#001C2A" Font-Bold="True" ForeColor="White" />
                  <PagerStyle BackColor="#001C2A" ForeColor="White" HorizontalAlign="Center" />
                  <RowStyle ForeColor="#333333" BackColor="#f9f9f9" />
                  <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                  <SortedAscendingCellStyle BackColor="#E9E7E2" />
                  <SortedAscendingHeaderStyle BackColor="#506C8C" />
                  <SortedDescendingCellStyle BackColor="#FFFDF8" />
                  <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
              </asp:GridView>

Open in new window

0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
Imran Javed ZiaConsultant Software Engineer - .NET ArchitectCommented:

<asp:BoundField DataField="Title" HeaderText="Topic" SortExpression="important, Title" />
0
 
runnerjp2005Author Commented:
would this mean the top row will be the important oneS?
0
 
Imran Javed ZiaConsultant Software Engineer - .NET ArchitectCommented:
Yes
0
 
runnerjp2005Author Commented:
This just adds anouther coloum!

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
                  AutoGenerateColumns="False" CellPadding="4" DataKeyNames="ID" 
                  DataSourceID="AccessDataSource1" ForeColor="#333333" GridLines="None" 
                  Width="970px">
                  <AlternatingRowStyle BackColor="#f9f9f9" ForeColor="Black" />
                  <Columns>
                  <asp:BoundField DataField="Title" HeaderText="Topic" SortExpression="important, Title" /> 
                      <asp:TemplateField>
                      <ItemTemplate>   
 <asp:Image ID="key" runat="server" />
    </ItemTemplate>   
                      </asp:TemplateField>
                      <asp:BoundField DataField="Title" HeaderText="Topic" SortExpression="Title" />
                      <asp:BoundField DataField="Auther" HeaderText="Topic Starter" 
                          SortExpression="Auther" />
                   <asp:TemplateField HeaderText="Replies" >   
<ItemTemplate>   
  <asp:Label ID="replies" runat="server" />
    </ItemTemplate>   
</asp:TemplateField>

                  <asp:TemplateField HeaderText="Last Post" >   
<ItemTemplate>   
  <asp:Label ID="lastrep" runat="server" />
    </ItemTemplate>   
</asp:TemplateField>

 <asp:BoundField DataField="views" HeaderText="Views" SortExpression="views" />

                  </Columns>
                  <EditRowStyle BackColor="#999999" />
                  <FooterStyle BackColor="#001C2A" ForeColor="White" Font-Bold="True" />
                  <HeaderStyle BackColor="#001C2A" Font-Bold="True" ForeColor="White" />
                  <PagerStyle BackColor="#001C2A" ForeColor="White" HorizontalAlign="Center" />
                  <RowStyle ForeColor="#333333" BackColor="#f9f9f9" />
                  <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                  <SortedAscendingCellStyle BackColor="#E9E7E2" />
                  <SortedAscendingHeaderStyle BackColor="#506C8C" />
                  <SortedDescendingCellStyle BackColor="#FFFDF8" />
                  <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
              </asp:GridView>

Open in new window

0
 
CodeCruiserCommented:
Just use the GridView.Sort method

GridView1.Sort("Important, Date", SortDirection.Ascending)


http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sort.aspx
0
 
runnerjp2005Author Commented:
i added it to my page load
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim s As String
        s = Request.QueryString("fn")
        Label1.Text = s

        GridView1.Sort("important, lasttime", SortDirection.Ascending)




    End Sub

Open in new window

and it does not add the important to the top... to note important is ticked in a check box within access and lasttime is date 03/12/2011 07:47:13 and i would like newest posts at the top..

so it would look like this

important
important
03/12/2011 07:47:13
03/12/2011 07:4313
03/12/2011 06:33:13
0
 
CodeCruiserCommented:
I thought i would look like this

IsImportant    Date
Yes              03/12/2011 06:33:13
No                03/12/2011 07:47:13
No                03/12/2011 07:43:13
0
 
runnerjp2005Author Commented:
no what its supposed to do is act in a forum where you add the "stickys or imortant" topics to the top.

An example of this can be found here http://www.phpfreaks.com/forums/index.php/board,1.0.html

as you can see its bown where in important fields are.
0
 
CodeCruiserCommented:
Its using two columns and if you sort your grid by "Important, lasttime" with Descending, it should do the job.
0
 
runnerjp2005Author Commented:
so do i still need to add a colom with improtant in it and just leave it blank?
0
 
CodeCruiserCommented:
> to note important is ticked in a check box within access
>so do i still need to add a colom with improtant in it and just leave it blank?

Why would you leave it blank? How would the grid know if a post is important or not without the important column in the grid?
0
 
runnerjp2005Author Commented:
thats a good point... i was going to colour the grid row and i suppose add an image to indicate this.

i have tried this and it still does not add the improtant to the top

forum.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="forums.aspx.vb" Inherits="Default2" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <link href="../Styles/Site.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="../Scripts/jquery-1.4.1.js"></script>
 <script type="text/javascript" src="../Javascript/imagecycle.js"></script>
 <script type="text/javascript" src="../Javascript/curvycorners.js"></script>

 </head>
<body>
    <form id="form1" runat="server">
     <div id="top-page-header"><div id="head-wrapper">
  <div id="site-logo">
        <asp:Image ID="Image1" runat="server" 
            ImageUrl="~/images/runningprofilestext.gif" />
        <asp:Image ID="Image2" runat="server" ImageUrl="~/images/rplogo.gif" />
  </div>
      </div></div>
    <div class ="largeMain">
          <div id="maininfolarge" class="forumtd">
              <h2>
                  <strong>Welcome To The <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>&nbsp;Forum&nbsp;
                  </strong>
                     </h2>
                  <div style="float:right">
                      New Topic
                      </div>
           
          
             
              <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
                  AutoGenerateColumns="False" CellPadding="4" DataKeyNames="ID" 
                  DataSourceID="AccessDataSource1" ForeColor="#333333" GridLines="None" 
                  Width="970px">
                  <AlternatingRowStyle BackColor="#f9f9f9" ForeColor="Black" />
                  <Columns>
                                      <asp:TemplateField>
                      <ItemTemplate>   
 <asp:Image ID="key" runat="server" />
    </ItemTemplate>   
                      </asp:TemplateField>
                      <asp:BoundField DataField="Title" HeaderText="Topic" SortExpression="Title" />
                                   <asp:BoundField DataField="important" HeaderText="" SortExpression="important" />
                      <asp:BoundField DataField="Auther" HeaderText="Topic Starter" 
                          SortExpression="Auther" />
                   <asp:TemplateField HeaderText="Replies" >   
<ItemTemplate>   
  <asp:Label ID="replies" runat="server" />
    </ItemTemplate>   
</asp:TemplateField>

                  <asp:TemplateField HeaderText="Last Post" >   
<ItemTemplate>   
  <asp:Label ID="lastrep" runat="server" />
    </ItemTemplate>   
</asp:TemplateField>

 <asp:BoundField DataField="views" HeaderText="Views" SortExpression="views" />

                  </Columns>
                  <EditRowStyle BackColor="#999999" />
                  <FooterStyle BackColor="#001C2A" ForeColor="White" Font-Bold="True" />
                  <HeaderStyle BackColor="#001C2A" Font-Bold="True" ForeColor="White" />
                  <PagerStyle BackColor="#001C2A" ForeColor="White" HorizontalAlign="Center" />
                  <RowStyle ForeColor="#333333" BackColor="#f9f9f9" />
                  <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                  <SortedAscendingCellStyle BackColor="#E9E7E2" />
                  <SortedAscendingHeaderStyle BackColor="#506C8C" />
                  <SortedDescendingCellStyle BackColor="#FFFDF8" />
                  <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
              </asp:GridView>
                                <asp:AccessDataSource ID="AccessDataSource1" runat="server"
                  DataFile="~/forums/forum.mdb" SelectCommand="SELECT * FROM [forum] WHERE Forum=@forum AND numrep = 0">
        <SelectParameters>
          <asp:QueryStringParameter Name="forum" QueryStringField="fn" />
        </SelectParameters>
              </asp:AccessDataSource><br />
                 </div>
                     
                     <div id="forumstats">
                     </div> 
                     
    </div>
   
    </form>
</body>
</html>

Open in new window


and forum.apsx.vb

Imports System.Data.SqlClient
Imports System.Web.Configuration
Imports System.Web
Imports System.Data
Imports Class1

Partial Class Default2
    Inherits System.Web.UI.Page




    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim s As String
        s = Request.QueryString("fn")
        Label1.Text = s


        GridView1.Sort("important, lasttime", SortDirection.Ascending)



    End Sub





    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then

            e.Row.Cells(0).Width = New Unit(50, UnitType.Pixel)

            e.Row.Cells(1).Width = New Unit(500, UnitType.Pixel)

            e.Row.Cells(2).Width = New Unit(100, UnitType.Pixel)
            e.Row.Cells(3).Width = New Unit(100, UnitType.Pixel)
            e.Row.Cells(4).Width = New Unit(220, UnitType.Pixel)
            
            If e.Row.RowIndex <> -1 Then
                e.Row.Cells(0).BackColor = Drawing.Color.White

                e.Row.Cells(4).BackColor = Drawing.Color.White
                e.Row.Cells(2).BackColor = Drawing.Color.White
                e.Row.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#FFFFFF'")

                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle;")
            End If

     

            Dim s As String = e.Row.DataItem("Title")
            Dim Res As Results = Class1.lastpost(s)
            Dim lastrep As Label = CType(e.Row.FindControl("lastrep"), Label)
            Dim lastpostername As String
            If Res.Result3.Split("|")(1) = "runnerjp" Then

                lastpostername = "<font color='red'><b>" & Res.Result3.Split("|")(1) & "</b></font>"
            Else
                lastpostername = "<b>" & Res.Result3.Split("|")(1) & "</b>"
            End If
            lastrep.Text = "By <b>" & lastpostername & "</b> <br /> " & timeago.GetDifferenceDate(Res.Result3.Split("|")(2), DateAndTime.Now)

            If e.Row.DataItem("Auther") = "runnerjp" Then
                e.Row.Cells(2).ForeColor = Drawing.Color.Red
            End If


            Dim replies As Label = CType(e.Row.FindControl("replies"), Label)
            replies.Text = Res.Result2
            Dim key As Image = CType(e.Row.FindControl("key"), Image)

            If e.Row.DataItem("forumlock") = False Then

                If Res.Result2 < 15 Then
                    key.ImageUrl = "~/images/thread.gif"
                Else
                    key.ImageUrl = "~/images/thread_hot.gif"
                End If

            Else
                key.ImageUrl = "~/images/thread_lock.gif"
            End If



            e.Row.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#FFFFFF'")

            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle;")








        End If

    End Sub


End Class

Open in new window

0
 
CodeCruiserCommented:
So important is a column in Forum table? What's its datatype? You need to convert it to Boolean. Or may be 0/1 where 0 will be no and 1 will be yes.
0
 
runnerjp2005Author Commented:
it was a yes/no tick box in access db  to true/false

i have now set it to number where 0 will be no and 1 will be yes.  but it still does not do it.

I have added a screen shot below
untitled.bmp
0
 
CodeCruiserCommented:
>GridView1.Sort("important, lasttime", SortDirection.Ascending)

Its because your sort direction is ascending. Change to descending.
0
 
runnerjp2005Author Commented:
GridView1.Sort("important, lasttime", SortDirection.Descending)

still the same
0
 
CodeCruiserCommented:
Add a few more enteries so that we can judge it better. Also, try calling DataBind() after sort.
0
 
runnerjp2005Author Commented:
ok i added a few more entrys and swapped
GridView1.Sort("important, lasttime", SortDirection.Descending)
TO
GridView1.Sort("lasttime,important", SortDirection.Descending)


and found it only arranges the last one...

with GridView1.Sort("important, lasttime", SortDirection.Descending)

i have attached the following screen shot...
untitled.bmp
0
 
CodeCruiserCommented:
So its sorting Descending on Important field but its sorting Ascending on lasttime field.
0
 
runnerjp2005Author Commented:
yes correct... although im not 100% sure its sorting the lasttime field
0
 
CodeCruiserCommented:
I have no idea why its doing that. What is datatype of lasttime?
0
 
runnerjp2005Author Commented:
Date/Time
0
 
CodeCruiserCommented:
Try a test page with a numeric and a string column and see what are the results.
0
 
runnerjp2005Author Commented:
ok i did it with just ID and important and it seems to be that
GridView1.Sort("important", SortDirection.Ascending)
&
GridView1.Sort("lasttime", SortDirection.Ascending)

works but if i do

GridView1.Sort("important, lasttime", SortDirection.Ascending)

neither work!!!
0
 
runnerjp2005Author Commented:
sorry lasttime is ID
0
 
Mohamed AbowardaSoftware EngineerCommented:
Set "Important" database field data type to integer, if it's important the field value will be 1, if not the value remain be 0.

Then you can execute SQL query as the following:
SELECT * FROM TableName ORDER by important DESC

Open in new window

0
 
runnerjp2005Author Commented:
Medo3337:

i want to first sort by important

then sor all the 1's by date
and all the 2's by date
0
 
runnerjp2005Author Commented:
i did not think if just using the select query for it... amazing!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.