Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2011-05-03
31
Medium Priority
?
415 Views
Last Modified: 2012-06-21
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??
0
Comment
Question by:runnerjp2005
  • 16
  • 10
  • 3
  • +1
31 Comments
 
LVL 16

Expert Comment

by:Imran Javed Zia
ID: 35514460
Yes,  you can use multi column sort by manually handling sort expresion
and use following expresion
important, dateField
0
 

Author Comment

by:runnerjp2005
ID: 35514543
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
 
LVL 16

Expert Comment

by:Imran Javed Zia
ID: 35516613

<asp:BoundField DataField="Title" HeaderText="Topic" SortExpression="important, Title" />
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:runnerjp2005
ID: 35517144
would this mean the top row will be the important oneS?
0
 
LVL 16

Expert Comment

by:Imran Javed Zia
ID: 35688247
Yes
0
 

Author Comment

by:runnerjp2005
ID: 35688328
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 35688347
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
 

Author Comment

by:runnerjp2005
ID: 35688505
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 35688530
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
 

Author Comment

by:runnerjp2005
ID: 35688569
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 35688589
Its using two columns and if you sort your grid by "Important, lasttime" with Descending, it should do the job.
0
 

Author Comment

by:runnerjp2005
ID: 35688629
so do i still need to add a colom with improtant in it and just leave it blank?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 35688643
> 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
 

Author Comment

by:runnerjp2005
ID: 35688704
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 35688748
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
 

Author Comment

by:runnerjp2005
ID: 35688851
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 35688934
>GridView1.Sort("important, lasttime", SortDirection.Ascending)

Its because your sort direction is ascending. Change to descending.
0
 

Author Comment

by:runnerjp2005
ID: 35688973
GridView1.Sort("important, lasttime", SortDirection.Descending)

still the same
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 35689018
Add a few more enteries so that we can judge it better. Also, try calling DataBind() after sort.
0
 

Author Comment

by:runnerjp2005
ID: 35689036
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 35689271
So its sorting Descending on Important field but its sorting Ascending on lasttime field.
0
 

Author Comment

by:runnerjp2005
ID: 35689279
yes correct... although im not 100% sure its sorting the lasttime field
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 35689296
I have no idea why its doing that. What is datatype of lasttime?
0
 

Author Comment

by:runnerjp2005
ID: 35689344
Date/Time
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 35689356
Try a test page with a numeric and a string column and see what are the results.
0
 

Author Comment

by:runnerjp2005
ID: 35689467
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
 

Author Comment

by:runnerjp2005
ID: 35689471
sorry lasttime is ID
0
 
LVL 12

Expert Comment

by:Mohamed Abowarda
ID: 35689549
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
 

Author Comment

by:runnerjp2005
ID: 35689619
Medo3337:

i want to first sort by important

then sor all the 1's by date
and all the 2's by date
0
 
LVL 12

Accepted Solution

by:
Mohamed Abowarda earned 2000 total points
ID: 35689663
SELECT * FROM TableName ORDER by important DESC, date DESC

Open in new window


Change "date" to your date field name.
0
 

Author Closing Comment

by:runnerjp2005
ID: 35690088
i did not think if just using the select query for it... amazing!!!
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses
Course of the Month13 days, 14 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question