GridView - Sorting

liunx

Guest
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData("CustomerID");
}
}

public void BindData(string SortField)
{
SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString);
SqlCommand myCommand = new SqlCommand();
myCommand.Connection = cnn;
myCommand.CommandText = "SELECT * FROM Customer";
myCommand.CommandType = CommandType.Text;
SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
DataSet ds = new DataSet();
myAdapter.Fill(ds, "Customer");

DataView Source = ds.Tables[0].DefaultView;
Source.Sort = SortField;

GridView1.DataSource = Source;
GridView1.DataBind();
}

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{

}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindData("");
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindData("");
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int customerID;
string customerName;

customerID = int.Parse(((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text);
customerName = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;

SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString);
SqlCommand myCommand = new SqlCommand();
myCommand.Connection = cnn;
myCommand.CommandText = "UPDATE Customer SET CustomerName='" + customerName + "' WHERE CustomerID=" + customerID;
SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
DataSet ds = new DataSet();
myAdapter.Fill(ds, "Customer");
GridView1.EditIndex = -1;
BindData("");
}

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
if (e.SortDirection == SortDirection.Ascending)
{
BindData((e.SortExpression).ToString() + " desc");
}
else
{
BindData((e.SortExpression).ToString() + " asc");
}
}
}


I created a GridView control and did the coding for the update and cancel function. I am trying to implement the sorting but im having a problem with my sorting coding:


protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
if (e.SortDirection == SortDirection.Ascending)
{
BindData((e.SortExpression).ToString() + " desc");
}
else
{
BindData((e.SortExpression).ToString() + " asc");
}
}


I can click on the header of the column and it sorts one time. The second time I click on the header of the column it does not work. Meaning from asc to desc and desc to asc and vice versa.

How do I solve this problem?

Your help is kindly appreciated.
 
Back
Top