Wrox Programmer Forums
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old May 21st, 2004, 05:31 AM
Registered User
Join Date: Mar 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default DataGrid Sorting

I am using a datagrid which has properties AllowSorting="True" and AutoGenerateColumns="False" .

But it is not showing Hyperlinks on the columns header.

Old May 22nd, 2004, 12:21 AM
Friend of Wrox
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik

This is sample for that, HTH!
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<Script Runat="Server">
  String sortField = "ProductID";

  void Page_Load( Object s, EventArgs e )
    if ( !IsPostBack )

  void BindData()
    SqlConnection myConnection;
    SqlCommand myCommand;
    string sqlString;
    myConnection = new SqlConnection( "Server=Localhost;uid=sa;Database=Northwind" );
    sqlString = "Select * from Products Order By " + sortField;
    myCommand = new SqlCommand( sqlString, myConnection );
    myDataGrid.DataSource = myCommand.ExecuteReader();

  void SortGrid( Object s, DataGridSortCommandEventArgs e )
    sortField = e.SortExpression;


<form Runat="Server">
<asp:DataGrid id="myDataGrid" AllowSorting="True" onSortCommand="SortGrid"
  cellpadding=1 Runat="Server"/>

Hovik Melkomian.
Old May 22nd, 2004, 03:42 AM
Friend of Wrox
Join Date: Oct 2003
Posts: 336
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alyeng2000

Continue to What Melvik Said
when AutoGenerateColumns="False"
So You Will Create The Bounded Columns So....
you will adjust each field SortExpression value to be The Field Name
like follows:
<asp:BoundColumn DataField="CategoryID" SortExpression="CategoryID" HeaderText="CategoryID"></asp:BoundColumn>

Ahmed Ali
Software Developer
Old May 26th, 2004, 08:52 AM
Registered User
Join Date: Mar 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts

Thanks melvik & alyeng2000
   But i m getting error

AllowCustomPaging must be true and VirtualItemCount must be set for a DataGrid with ID DataGrid1 when AllowPaging is set to true and the selected datasource does not implement ICollection.


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using DriveConnection;

namespace DRIVE.Test
    /// <summary>
    /// Summary description for TestDG.
    /// </summary>
    public class TestDG : System.Web.UI.Page
        protected System.Web.UI.WebControls.DataGrid DataGrid1;
        string SortField;

        private void Page_Load(object sender, System.EventArgs e)
            if (!IsPostBack)
                //SortField = "vchrQuarterID";
                if (SortField == null)
                    SortField = "intSegmentProposalID";



        void BindGrid()

            DBConn cn = new DBConn();
            SqlConnection cnn = cn.openConnection();

            string strText;

            strText = "Myprocedure '" + SortField + "'";
            SqlCommand cmd = new SqlCommand(strText,cnn);
            SqlDataReader drSegRpt = cmd.ExecuteReader();
            //DataGrid1.DataSource = drSegRpt;
            SqlCommand cmd1 = new SqlCommand(strText,cnn);
            SqlDataReader drSegRpt1 = cmd1.ExecuteReader();

            DataGrid1.DataSource = drSegRpt1;

        private void DataGrid1_Sort(object source, System.Web.UI.WebControls.DataGridSortCommandEvent Args e)
            SortField = (string)e.SortExpression;

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
            this.DataGrid1.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEvent Handler(this.DataGrid1_Sort);
            this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexCh anged);
            this.Load += new System.EventHandler(this.Page_Load);


        private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)



<%@ Page language="c#" Codebehind="TestDG.aspx.cs" AutoEventWireup="false" Inherits="DRIVE.Test.TestDG" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
        <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
        <meta name="CODE_LANGUAGE" Content="C#">
        <meta name="vs_defaultClientScript" content="JavaScript">
        <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    <body MS_POSITIONING="GridLayout">
        <form id="Form1" method="post" runat="server">
            <asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" runat="server"
                AllowSorting="True" BorderColor="Silver" Font-Names="Verdana" Font-Size="Smaller" AutoGenerateColumns="False"
                <HeaderStyle Font-Size="Smaller" Font-Names="Verdana" Font-Bold="True" BackColor="Silver"></HeaderStyle>
                    <asp:BoundColumn DataField="vchrQuarterId" SortExpression="vchrQuarterId" HeaderText="Fiscal Quarter"></asp:BoundColumn>
                    <asp:BoundColumn DataField="vchrVendorName" SortExpression="vchrVendorName" HeaderText="Vendor"></asp:BoundColumn>


Similar Threads
Thread Thread Starter Forum Replies Last Post
Datagrid sorting Amorous ASP.NET 1.x and 2.0 Application Design 1 September 30th, 2005 11:44 PM
Sorting a datagrid trekmp ADO.NET 7 December 18th, 2004 06:56 AM
Datagrid sorting by non alphabetical sorting? LLAndy VS.NET 2002/2003 1 July 15th, 2004 01:20 AM
Sorting a Datagrid - Help Please! Pauline VS.NET 2002/2003 0 August 29th, 2003 06:20 AM
DataGrid Sorting Example SPRIBob BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 10 July 30th, 2003 09:48 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.