How to bring paging, sorting and hyperlinking functioanlities in a datagrid?

C

Crazy Learner

Hi there,
I just started asp.net code 3 days ago from now.
I have a situation in one of my pages that i need to provide all three
functionailities paging, sorting all of the columns and hyperlinking of
the two columns in the datagrid. I am very glad that
i could page and sort and enjoyed the result.
inorder to hyperlink couple of columns in the
datagrid i made AutoGenerateColumns="False"
(if i don't set this value "False" the datagrid displaying duplicate
columns) and used
<asp:hyperlinkcolumn> to the column i want
hyperlink and <asp:boundcolumn> for the rest of the
columns to display them asusual in the datagrid. I
am able to hyperlink the mentioned columns in the
order i wanted to and also able to page the results now.
But unfortunately i lost sorting functionality. Now i
am only able to page and hyperlink but not sorting.
Hope you got where i am strucking at. Please help me
to get out of this problem.thanks much and appreciate
your encouragement for the new programers :) thanks
once again. I am pasting my Html and code behind pages
underneath. i am just using customers table in
northwind database in sqlserver 2000.
*************** Html part of the.aspx***************

<%@ Register TagPrefix="mbrsc"
Namespace="MetaBuilders.WebControls"
Assembly="MetaBuilders.WebControls.RowSelectorColumn"
%>
<%@ Page Language="vb" Src="project_listing.aspx.vb"
Inherits="project_listing" %>
<html>
<head>
<title>Paging and Sorting in a DataGrid</title>
<meta name="GENERATOR" content="Microsoft Visual
Studio.NET 7.0">
<meta name="CODE_LANGUAGE" content="Visual Basic
7.0">
<meta name=vs_defaultClientScript
content="JavaScript">
<meta name=vs_targetSchema

content="http://schemas.microsoft.com/intellisense/ie5"
</head>
<body>
<h3 align=center><font face="Verdana">Project
Listing</font></h3>
<form runat="server">

<table>
<tr><td>
<asp:radiobutton id="Radiobutton1" runat="server"
groupname="projectselection"></asp:radiobutton>My
</td>
<td>Projects
<asp:radiobutton id="Radiobutton2" runat="server"
groupname="projectselection"></asp:radiobutton>Shared
</td>
<td>Projects
<asp:radiobutton id="Radiobutton3" runat="server"
groupname="projectselection"
checked="true"></asp:radiobutton>All</td>
</table>
<p></p>
<P></P>
<asp:Label id="lblOrderBy" runat="server"
Visible="False" />
<asp:DataGrid id="dtgCusts"
align="center"
AllowPaging="True"

OnPageIndexChanged="PageIndexChanged_Click"
PageSize="10"
PagerStyle-Mode="NumericPages"
PagerStyle-Position="Top"
PagerStyle-HorizontalAlign="Center"
PagerStyle-CssClass="pageLinks"
AutoGenerateColumns="False"
AllowSorting="True"
OnSortCommand="SortCommand_Click"

runat="server">
<Columns>
<mbrsc:RowSelectorColumn selectionMode="Single"
/>
<asp:boundcolumn headertext="Company Name"
datafield="CompanyName" />
<asp:HyperLinkColumn DataTextField="Contactname"
HeaderText="Contact Name"
DataNavigateUrlField="contactname"

datanavigateurlformatstring="mainIndex.aspx?Id={0}>"
/>
<asp:boundcolumn headertext="ContactTitle"
datafield="ContactTitle" />
</Columns>
</asp:datagrid>

<p></p>
<p></p>


<table align="center" border="0">
<tr>
<td><asp:button id="tbnNew" text="New"
runat="server" /></td>
<td> </td>
<td><asp:button id="btnAssign" text="Assign"
runat="server" /></td>
<td> </td>
<td><asp:button id="btnDelete" text="Delete"
runat="server" /></td>
</tr>
</table>
</form>
</body>
</html>
***************End of the html part .aspx

***** code behind code part of the
.aspx.vb**********
Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Public Class project_listing : Inherits Page

Protected dtgCusts As DataGrid
Protected lblOrderBy As Label
Protected strSql As String = "SELECT
CompanyName,
ContactName, ContactTitle, Phone FROM Customers"

Sub Page_Load(ByVal sender As Object, ByVal e As
EventArgs)
If Not Page.IsPostBack Then
BindTheData()
End If
End Sub

Sub BindTheData()
'Dim strConn As String
'strConn =

ConfigurationSettings.AppSettings"NorthwindConnection")

Dim strConn As String =
"server=movva;database=NorthWind;user
id=sa;password=pradev"
strSql = strSql + lblOrderBy.Text
Dim myDataSet As DataSet = New DataSet()
Dim myDataAdapter As SqlDataAdapter = New
SqlDataAdapter(strSql, strConn)
myDataAdapter.Fill(myDataSet, "Customers")

dtgCusts.DataSource =
myDataSet.Tables("Customers")

dtgCusts.DataBind()
End Sub

Sub PageIndexChanged_Click(ByVal sender As
Object,
ByVal e As DataGridPageChangedEventArgs)
dtgCusts.CurrentPageIndex = e.NewPageIndex
BindTheData()
End Sub

Sub SortCommand_Click(ByVal sender As Object,
ByVal e As DataGridSortCommandEventArgs)
lblOrderBy.Text = " ORDER BY " &
e.SortExpression
BindTheData()
End Sub

End Class

****************** end of the codebehind coe**********
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top