Click here to Skip to main content
15,911,646 members
Home / Discussions / C#
   

C#

 
GeneralRe: Add image to database Pin
Mazdak24-Feb-04 5:19
Mazdak24-Feb-04 5:19 
GeneralRe: Add image to database Pin
Heath Stewart24-Feb-04 5:23
protectorHeath Stewart24-Feb-04 5:23 
GeneralRe: Add image to database Pin
turbochimp25-Feb-04 10:58
turbochimp25-Feb-04 10:58 
Generalmusic files Pin
jphuphilly24-Feb-04 4:22
jphuphilly24-Feb-04 4:22 
GeneralRe: music files Pin
Mazdak24-Feb-04 5:23
Mazdak24-Feb-04 5:23 
GeneralRe: music files Pin
jphuphilly24-Feb-04 7:03
jphuphilly24-Feb-04 7:03 
GeneralCrystal Report batch console application Pin
Visionsoft24-Feb-04 3:53
Visionsoft24-Feb-04 3:53 
GeneralSorting DataGrid BoundColumn using DataGrid.SortCommand Pin
ajaylulia24-Feb-04 2:21
ajaylulia24-Feb-04 2:21 
Sorting DataGrid BoundColumn using DataGrid.SortCommand

I have DataGrid control and I am adding BoundColumns at runtime.

My Questions are:
1) I need to sort DataGrid using DataGrid.SortCommand and **NOT** OnSortCommand as I am using CodeBack file.
2) I also need to Hide last column.

Below is code for aspx file


<%@ Page language="c#" Codebehind="DataGridSortingDemo.aspx.cs" AutoEventWireup="false" Inherits="Connection.DataGridSortingDemo" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
	<HEAD>
		<title>DataGridSortingDemo</title>
		<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
		<meta name="CODE_LANGUAGE" Content="C#">
		<meta name="vs_defaultClientScript" content="JavaScript">
		<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
	</HEAD>
	<body>
		<form id="DataGridSortingDemo" method="post" runat="server">
			<asp:DataGrid id="DataGrid1" runat="server" AllowSorting="True" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="3">
				<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#669999"></SelectedItemStyle>
				<ItemStyle ForeColor="#000066"></ItemStyle>
				<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#006699"></HeaderStyle>
				<FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle>
				<Columns>
					<asp:TemplateColumn>
						<ItemTemplate>
							<asp:CheckBox id="CheckBox1" runat="server"></asp:CheckBox>
						</ItemTemplate>
					</asp:TemplateColumn>
				</Columns>
				<PagerStyle HorizontalAlign="Left" ForeColor="#000066" BackColor="White" Mode="NumericPages"></PagerStyle>
			</asp:DataGrid>
		</form>
	</body>
</HTML>





Below is code back (aspx.cs) file


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;

namespace Connection
{
	/// <summary>
	/// Summary description for DataGridSortingDemo.
	/// </summary>
	public class DataGridSortingDemo : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.DataGrid DataGrid1;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			if( ! IsPostBack)
			{
				DataGrid1.DataSource = CreateDataSource();
				DataGrid1.DataBind();
			}
		}

		protected DataView CreateDataSource()
		{
			string strConnectionString = "user id=sa;password=;Database=Northwind;Server=localhost";
			string strSQLQuery = "SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City, 1 FROM Customers";
			DataTable objDT = new DataTable();
			BoundColumn objBoundColumn;

			SqlConnection objSQLConn = new SqlConnection(strConnectionString);
			SqlDataAdapter objSQLDA = new SqlDataAdapter(strSQLQuery, objSQLConn);

			objSQLConn.Open();
			objSQLDA.Fill(objDT);
			objSQLConn.Close();
			
			DataGrid1.AutoGenerateColumns = false;			
			for (int intCounter = 0; intCounter < objDT.Columns.Count; intCounter++)
			{
				objBoundColumn = new BoundColumn();
				objBoundColumn.HeaderText = objDT.Columns[intCounter].ColumnName;
				objBoundColumn.DataField  = objDT.Columns[intCounter].ColumnName;
				objBoundColumn.SortExpression = objDT.Columns[intCounter].ColumnName;
				DataGrid1.Columns.Add(objBoundColumn);
			}

			DataView dvCommonDataView = new DataView(objDT);

			return dvCommonDataView;
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <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.DataGridSortCommandEventHandler(this.DataGrid1_SortCommand);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
		{
			Response.Write("Hi There");
		}

	}
}

GeneralRe: Sorting DataGrid BoundColumn using DataGrid.SortCommand Pin
Mazdak24-Feb-04 3:21
Mazdak24-Feb-04 3:21 
GeneralRe: Sorting DataGrid BoundColumn using DataGrid.SortCommand Pin
ajaylulia24-Feb-04 16:56
ajaylulia24-Feb-04 16:56 
GeneralAsynchronous sockets and message queues Pin
Rickard Andersson2024-Feb-04 2:08
Rickard Andersson2024-Feb-04 2:08 
GeneralRe: Windows Processes&amp; Properties Pin
Heath Stewart24-Feb-04 5:05
protectorHeath Stewart24-Feb-04 5:05 
GeneralError: Malformed server response Pin
rosie224-Feb-04 0:33
rosie224-Feb-04 0:33 
GeneralRe: Error: Malformed server response Pin
Heath Stewart24-Feb-04 5:03
protectorHeath Stewart24-Feb-04 5:03 
GeneralDirectX Mesh Pin
SherKar23-Feb-04 23:08
SherKar23-Feb-04 23:08 
GeneralRe: DirectX Mesh Pin
Heath Stewart24-Feb-04 4:59
protectorHeath Stewart24-Feb-04 4:59 
Questioncontrols in a ListView??? Pin
Snowjim23-Feb-04 22:19
Snowjim23-Feb-04 22:19 
AnswerRe: controls in a ListView??? Pin
Heath Stewart24-Feb-04 4:56
protectorHeath Stewart24-Feb-04 4:56 
QuestionHow to get Log of all files used for a day Pin
Member 33502223-Feb-04 20:30
Member 33502223-Feb-04 20:30 
AnswerRe: How to get Log of all files used for a day Pin
Mazdak24-Feb-04 0:38
Mazdak24-Feb-04 0:38 
AnswerRe: How to get Log of all files used for a day Pin
Heath Stewart24-Feb-04 4:54
protectorHeath Stewart24-Feb-04 4:54 
QuestionHow to implement IDocHostUIHandler with c# ? Pin
benzite23-Feb-04 19:47
benzite23-Feb-04 19:47 
AnswerRe: How to implement IDocHostUIHandler with c# ? Pin
Heath Stewart24-Feb-04 4:50
protectorHeath Stewart24-Feb-04 4:50 
AnswerRe: How to implement IDocHostUIHandler with c# ? Pin
LongRange.Shooter24-Feb-04 11:00
LongRange.Shooter24-Feb-04 11:00 
GeneralRe: How to implement IDocHostUIHandler with c# ? Pin
benzite24-Feb-04 14:37
benzite24-Feb-04 14:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.