Eradicate
November 30th, 2004, 02:24 AM
I have some problem with sorting in multiple columns. I tried duplicating the the events etc but it still won't work. It tends to only sort by one expression. For example, i have 2 columns "name" and "company". When i click name, it will sort in ascending order of the names, which is correct but when i tried sorting by company, it sorts by name which isn't correct.
The code below is for sorting only by name.
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
ViewState["sortField"] = "Name";
ViewState["sortDirection"] = "DESC";
BindGrid();
}
}
private void BindGrid()
{
SqlCommand cmd = sqlConnection1.CreateCommand();
cmd.CommandText = "SELECT * FROM Factory.Company ORDER BY " + ViewState["sortField"].ToString() + " " + ViewState["sortDirection"].ToString();
SqlDataAdapter sqlDataAdapter11 = new SqlDataAdapter();
sqlDataAdapter11.SelectCommand = cmd;
DataSet ds = new DataSet();
sqlConnection1.Open();
sqlDataAdapter11.Fill(ds, "Company");
sqlConnection1.Close();
DataGrid5.DataSource = ds;
DataGrid5.DataBind();
}
private void DataGrid5_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid5.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}
private void DataGrid5_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
if(e.SortExpression.ToString() == ViewState["sortField"].ToString())
{
switch(ViewState["sortDirection"].ToString())
{
case "ASC":
ViewState["sortDirection"] = "DESC";
break;
case "DESC":
ViewState["sortDirection"] = "ASC";
break;
}
}
else
{
ViewState["sortField"] = e.SortExpression;
ViewState["sortDirection"] = "DESC";
}
BindGrid();
}
What should be added in order to make multi column sorting work? or i have to re-write the whole program?
Help needed thanks in advance
-John
The code below is for sorting only by name.
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
ViewState["sortField"] = "Name";
ViewState["sortDirection"] = "DESC";
BindGrid();
}
}
private void BindGrid()
{
SqlCommand cmd = sqlConnection1.CreateCommand();
cmd.CommandText = "SELECT * FROM Factory.Company ORDER BY " + ViewState["sortField"].ToString() + " " + ViewState["sortDirection"].ToString();
SqlDataAdapter sqlDataAdapter11 = new SqlDataAdapter();
sqlDataAdapter11.SelectCommand = cmd;
DataSet ds = new DataSet();
sqlConnection1.Open();
sqlDataAdapter11.Fill(ds, "Company");
sqlConnection1.Close();
DataGrid5.DataSource = ds;
DataGrid5.DataBind();
}
private void DataGrid5_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid5.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}
private void DataGrid5_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
if(e.SortExpression.ToString() == ViewState["sortField"].ToString())
{
switch(ViewState["sortDirection"].ToString())
{
case "ASC":
ViewState["sortDirection"] = "DESC";
break;
case "DESC":
ViewState["sortDirection"] = "ASC";
break;
}
}
else
{
ViewState["sortField"] = e.SortExpression;
ViewState["sortDirection"] = "DESC";
}
BindGrid();
}
What should be added in order to make multi column sorting work? or i have to re-write the whole program?
Help needed thanks in advance
-John