chapter 8:datagrid sorting not working
hi friends,
I have written the code as below for sorting the datagrid columns.but its not working.please check the code and help me to overcome my problem..
************************************************** *****************
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace WebApplication1.images
{
public class sort : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack )
{
BindGrid();
}
}
#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.Load += new System.EventHandler(this.Page_Load);
}
#endregion
public void DataGrid_Page(object sender, DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}
public void DataGrid_Sort(object sender, DataGridSortCommandEventArgs e)
{
DataGrid1.CurrentPageIndex = 0;
SortField = e.SortExpression;
BindGrid();
}
protected String SortField
{
get
{
object o = ViewState["SortField"];
return (o == null) ? String.Empty : (String)o;
}
set
{
ViewState["SortField"] = value;
}
}
void BindGrid()
{
SqlConnection con3=new SqlConnection("server=localhost;Persist Security Info=False;User ID=sa;password=sa;Initial Catalog=vms_database;Data Source=server0");
string CommandText;
if(SortField == String.Empty)
CommandText = "select * from Admin_ringtone order by songname";
else
CommandText = "select * from Admin_ringtone order by" + SortField;
SqlDataAdapter ad = new SqlDataAdapter("select * from Admin_ringtone ",con3);
DataSet ds = new DataSet();
ad.Fill(ds,"Admin_ringtone");
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
}
}
}
__________________
sumith
|