sorting a datagrid in descending order

G

Guest

i am able to allow the user to sort the columns of my datagrid, but only in
ascending order. is there a way to allow sorting in descending order? thanks
in advance.

private void Page_Load(object sender, System.EventArgs e)
{

string sTODS = "Select * From TaskOrder";
SqlConnection sqlConn = new SqlConnection(connStr);
SqlCommand sqlCMD = new SqlCommand(sTODS);
sqlCMD.Connection = sqlConn;
sqlConn.Open();
SqlDataAdapter adapter = new SqlDataAdapter(sqlCMD);
DataSet ds = new DataSet();
adapter.Fill(ds);
dgSearchResults.DataSource = ds.Tables[0];
int tableRowCount = ds.Tables[0].Rows.Count;
if (tableRowCount != 0)
{
DataView dv = new DataView();
dv=ds.Tables[0].DefaultView;
Page.Session.Add("DataViewOriginal",dv);
dgSearchResults.DataSource=dv;
dgSearchResults.DataBind();
dgSearchResults.Visible = true;
}
else
{
dgSearchResults.Visible = false;
}
}

private void dgSearchResults_SortCommand(object source,
System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
if(Page.Session["DataViewOriginal"]!=null)
{
DataView dv=(DataView) Page.Session["DataViewOriginal"];
dv.Sort=e.SortExpression;
dgSearchResults.DataSource=dv; //assign the source
dgSearchResults.DataBind();
}
}
 
I

Ignacio Machin \( .NET/ C# MVP \)

hi

I don;t know how you are declaring the datagrid itself, but all you have
to do is add an ASC or DESC to the SortExpression.

IIRC the SortCommand of the datagrid does not allow to especify it, so you
may will need to find another approach or a work around. like keeping in
session the last kind of sorting you did and just do the opposite.



let me know if you need some ideas.

cheers,
 
G

Guest

there are 8 columns (all sortable). i'm not sure using 8 session variables to
store everything is the way to go.

Ignacio Machin ( .NET/ C# MVP ) said:
hi

I don;t know how you are declaring the datagrid itself, but all you have
to do is add an ASC or DESC to the SortExpression.

IIRC the SortCommand of the datagrid does not allow to especify it, so you
may will need to find another approach or a work around. like keeping in
session the last kind of sorting you did and just do the opposite.



let me know if you need some ideas.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



Newbie said:
i am able to allow the user to sort the columns of my datagrid, but only in
ascending order. is there a way to allow sorting in descending order?
thanks
in advance.

private void Page_Load(object sender, System.EventArgs e)
{

string sTODS = "Select * From TaskOrder";
SqlConnection sqlConn = new SqlConnection(connStr);
SqlCommand sqlCMD = new SqlCommand(sTODS);
sqlCMD.Connection = sqlConn;
sqlConn.Open();
SqlDataAdapter adapter = new SqlDataAdapter(sqlCMD);
DataSet ds = new DataSet();
adapter.Fill(ds);
dgSearchResults.DataSource = ds.Tables[0];
int tableRowCount = ds.Tables[0].Rows.Count;
if (tableRowCount != 0)
{
DataView dv = new DataView();
dv=ds.Tables[0].DefaultView;
Page.Session.Add("DataViewOriginal",dv);
dgSearchResults.DataSource=dv;
dgSearchResults.DataBind();
dgSearchResults.Visible = true;
}
else
{
dgSearchResults.Visible = false;
}
}

private void dgSearchResults_SortCommand(object source,
System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
if(Page.Session["DataViewOriginal"]!=null)
{
DataView dv=(DataView) Page.Session["DataViewOriginal"];
dv.Sort=e.SortExpression;
dgSearchResults.DataSource=dv; //assign the source
dgSearchResults.DataBind();
}
}
 
I

Ignacio Machin \( .NET/ C# MVP \)

hi

you need only one.

or two at the most, one to decide which column and the other to decide
asc/desc if in a postback the column var is the same you change the asc/desc
if not change the column and do not change the asc/desc

is it clear now?

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Newbie said:
there are 8 columns (all sortable). i'm not sure using 8 session variables
to
store everything is the way to go.

Ignacio Machin ( .NET/ C# MVP ) said:
hi

I don;t know how you are declaring the datagrid itself, but all you
have
to do is add an ASC or DESC to the SortExpression.

IIRC the SortCommand of the datagrid does not allow to especify it, so
you
may will need to find another approach or a work around. like keeping in
session the last kind of sorting you did and just do the opposite.



let me know if you need some ideas.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



Newbie said:
i am able to allow the user to sort the columns of my datagrid, but only
in
ascending order. is there a way to allow sorting in descending order?
thanks
in advance.

private void Page_Load(object sender, System.EventArgs e)
{

string sTODS = "Select * From TaskOrder";
SqlConnection sqlConn = new SqlConnection(connStr);
SqlCommand sqlCMD = new SqlCommand(sTODS);
sqlCMD.Connection = sqlConn;
sqlConn.Open();
SqlDataAdapter adapter = new SqlDataAdapter(sqlCMD);
DataSet ds = new DataSet();
adapter.Fill(ds);
dgSearchResults.DataSource = ds.Tables[0];
int tableRowCount = ds.Tables[0].Rows.Count;
if (tableRowCount != 0)
{
DataView dv = new DataView();
dv=ds.Tables[0].DefaultView;
Page.Session.Add("DataViewOriginal",dv);
dgSearchResults.DataSource=dv;
dgSearchResults.DataBind();
dgSearchResults.Visible = true;
}
else
{
dgSearchResults.Visible = false;
}
}

private void dgSearchResults_SortCommand(object source,
System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
if(Page.Session["DataViewOriginal"]!=null)
{
DataView dv=(DataView) Page.Session["DataViewOriginal"];
dv.Sort=e.SortExpression;
dgSearchResults.DataSource=dv; //assign the source
dgSearchResults.DataBind();
}
}
 
G

Guest

i already fixed the problem - thanks

Ignacio Machin ( .NET/ C# MVP ) said:
hi

I don;t know how you are declaring the datagrid itself, but all you have
to do is add an ASC or DESC to the SortExpression.

IIRC the SortCommand of the datagrid does not allow to especify it, so you
may will need to find another approach or a work around. like keeping in
session the last kind of sorting you did and just do the opposite.



let me know if you need some ideas.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



Newbie said:
i am able to allow the user to sort the columns of my datagrid, but only in
ascending order. is there a way to allow sorting in descending order?
thanks
in advance.

private void Page_Load(object sender, System.EventArgs e)
{

string sTODS = "Select * From TaskOrder";
SqlConnection sqlConn = new SqlConnection(connStr);
SqlCommand sqlCMD = new SqlCommand(sTODS);
sqlCMD.Connection = sqlConn;
sqlConn.Open();
SqlDataAdapter adapter = new SqlDataAdapter(sqlCMD);
DataSet ds = new DataSet();
adapter.Fill(ds);
dgSearchResults.DataSource = ds.Tables[0];
int tableRowCount = ds.Tables[0].Rows.Count;
if (tableRowCount != 0)
{
DataView dv = new DataView();
dv=ds.Tables[0].DefaultView;
Page.Session.Add("DataViewOriginal",dv);
dgSearchResults.DataSource=dv;
dgSearchResults.DataBind();
dgSearchResults.Visible = true;
}
else
{
dgSearchResults.Visible = false;
}
}

private void dgSearchResults_SortCommand(object source,
System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
if(Page.Session["DataViewOriginal"]!=null)
{
DataView dv=(DataView) Page.Session["DataViewOriginal"];
dv.Sort=e.SortExpression;
dgSearchResults.DataSource=dv; //assign the source
dgSearchResults.DataBind();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top