sorting gridview question

  • Thread starter Thread starter JohnE
  • Start date Start date
J

JohnE

Hello. I have finally gotten the gridview to sort and page. But what I am
wondering is there a way to reverse the sort from what was originally done?
By that, if the user clicked the column header 'Name' it would sort (ASC) by
that column. If the user clicked on 'Name' again, the column would sort DESC
by that column. I also have an up arrow and down arrow that can be used to
show the column being sorted and in what direction. Where in code and how
would those be displayed? Here is the code that I have for the paging and
sorting. I was keeping it simple but it might be time to turn it up some.

private DataView bindgrid()
{
string connStr =
ConfigurationManager.ConnectionStrings["ProteusConnectionString"].ConnectionString;
string sql = "SEL_FillChangeRequestListGridView";
SqlDataAdapter sqlDa = new SqlDataAdapter(sql,connStr);
DataSet ds = new DataSet();
sqlDa.Fill(ds);
DataView dv = new DataView();

if (ViewState["sortExpr"] != null)
{ dv = new DataView(ds.Tables[0]);
dv.Sort = (string)ViewState["sortExpr"];
}
else
dv = ds.Tables[0].DefaultView;

return dv;
}

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataView dv = new DataView();
dv = bindgrid();
GridView1.DataSource = dv;
GridView1.DataBind();
}
}

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
ViewState["sortExpr"] = e.SortExpression;
GridView1.DataSource = bindgrid();
GridView1.DataBind();
}

protected void GridView1_PageIndexChanging(object sender,
GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource = bindgrid();
GridView1.DataBind();
}



I am a relatively new at all this coming from Access (VBA) to working on
webapps. So far I would say it is a great learning experience and wish I got
into it long ago.

Thanks... John
 
Hello.  I have finally gotten the gridview to sort and page.  But what I am
wondering is there a way to reverse the sort from what was originally done?  
By that, if the user clicked the column header 'Name' it would sort (ASC)by
that column.  If the user clicked on 'Name' again, the column would sort DESC
by that column.  I also have an up arrow and down arrow that can be used to
show the column being sorted and in what direction.  Where in code and how
would those be displayed?  Here is the code that I have for the paging and
sorting.  I was keeping it simple but it might be time to turn it up some.

    private DataView bindgrid()
    {          
        string connStr =
ConfigurationManager.ConnectionStrings["ProteusConnectionString"].Connectio­nString;
        string sql = "SEL_FillChangeRequestListGridView";
        SqlDataAdapter sqlDa = new SqlDataAdapter(sql,connStr);
        DataSet ds = new DataSet();        
        sqlDa.Fill(ds);
        DataView dv = new DataView();

        if (ViewState["sortExpr"] != null)            
        {                       dv = new DataView(ds.Tables[0]);                        
            dv.Sort = (string)ViewState["sortExpr"];          
        }              
        else                    
            dv = ds.Tables[0].DefaultView;      

        return dv;      
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DataView dv = new DataView();
            dv = bindgrid();
            GridView1.DataSource = dv;
            GridView1.DataBind();
        }
    }

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        ViewState["sortExpr"] = e.SortExpression;
        GridView1.DataSource = bindgrid();
        GridView1.DataBind();
    }

    protected void GridView1_PageIndexChanging(object sender,
GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataSource = bindgrid();
        GridView1.DataBind();
    }

I am a relatively new at all this coming from Access (VBA) to working on
webapps.  So far I would say it is a great learning experience and wishI got
into it long ago.

Thanks... John

Doing this

ViewState["sortExpr"] = e.SortExpression;

you keep the column name in the ViewState. You should add another
value to set sorting directrion. For example,

string sort = "Asc";
if(ViewState["sortExpr"] + "" != "" && ViewState["sortExpr"] + "" ==
e.SortExpression)
{
if(ViewState["sortDir"] + "" == "Asc")
sort = "Desc";
}
ViewState["sortExpr"] = e.SortExpression;
ViewState["sortDir"] = sort;

and then

dv.Sort = (string)ViewState["sortExpr"] + " " + ViewState["sortDir"];
 
Alexey, thanks for the info. I will give it a try and let you know the
result. I am assuming this added code goes into the Gridview1_Sorting?
John


Alexey Smirnov said:
Hello. I have finally gotten the gridview to sort and page. But what I am
wondering is there a way to reverse the sort from what was originally done?
By that, if the user clicked the column header 'Name' it would sort (ASC) by
that column. If the user clicked on 'Name' again, the column would sort DESC
by that column. I also have an up arrow and down arrow that can be used to
show the column being sorted and in what direction. Where in code and how
would those be displayed? Here is the code that I have for the paging and
sorting. I was keeping it simple but it might be time to turn it up some.

private DataView bindgrid()
{
string connStr =
ConfigurationManager.ConnectionStrings["ProteusConnectionString"].Connectio­nString;
string sql = "SEL_FillChangeRequestListGridView";
SqlDataAdapter sqlDa = new SqlDataAdapter(sql,connStr);
DataSet ds = new DataSet();
sqlDa.Fill(ds);
DataView dv = new DataView();

if (ViewState["sortExpr"] != null)
{ dv = new DataView(ds.Tables[0]);
dv.Sort = (string)ViewState["sortExpr"];
}
else
dv = ds.Tables[0].DefaultView;

return dv;
}

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataView dv = new DataView();
dv = bindgrid();
GridView1.DataSource = dv;
GridView1.DataBind();
}
}

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
ViewState["sortExpr"] = e.SortExpression;
GridView1.DataSource = bindgrid();
GridView1.DataBind();
}

protected void GridView1_PageIndexChanging(object sender,
GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource = bindgrid();
GridView1.DataBind();
}

I am a relatively new at all this coming from Access (VBA) to working on
webapps. So far I would say it is a great learning experience and wish I got
into it long ago.

Thanks... John

Doing this

ViewState["sortExpr"] = e.SortExpression;

you keep the column name in the ViewState. You should add another
value to set sorting directrion. For example,

string sort = "Asc";
if(ViewState["sortExpr"] + "" != "" && ViewState["sortExpr"] + "" ==
e.SortExpression)
{
if(ViewState["sortDir"] + "" == "Asc")
sort = "Desc";
}
ViewState["sortExpr"] = e.SortExpression;
ViewState["sortDir"] = sort;

and then

dv.Sort = (string)ViewState["sortExpr"] + " " + ViewState["sortDir"];
 
Alexey, I gave is a quick try and got the following error: 'DataTable must
be set prior to using DataView.' Below is the code and I added the Dataview
dv line for the dv.Sort. But the dv.Sort is where the error is occurring.
Taking baby steps here on this stuff. So, if you don't mind, explain a bit
more about this?
Thanks.
John


protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
string sort = "Asc";
DataView dv = new DataView();

if(ViewState["sortExpr"] + "" != "" && ViewState["sortExpr"] + "" ==
e.SortExpression)
{
if(ViewState["sortDir"] + "" == "Asc")
sort = "Desc";
}
ViewState["sortExpr"] = e.SortExpression;
ViewState["sortDir"] = sort;

dv.Sort = (string)ViewState["sortExpr"] + " " + ViewState["sortDir"];


//ViewState["sortExpr"] = e.SortExpression;
GridView1.DataSource = bindgrid();
GridView1.DataBind();
}



Alexey Smirnov said:
Hello. I have finally gotten the gridview to sort and page. But what I am
wondering is there a way to reverse the sort from what was originally done?
By that, if the user clicked the column header 'Name' it would sort (ASC) by
that column. If the user clicked on 'Name' again, the column would sort DESC
by that column. I also have an up arrow and down arrow that can be used to
show the column being sorted and in what direction. Where in code and how
would those be displayed? Here is the code that I have for the paging and
sorting. I was keeping it simple but it might be time to turn it up some.

private DataView bindgrid()
{
string connStr =
ConfigurationManager.ConnectionStrings["ProteusConnectionString"].Connectio­nString;
string sql = "SEL_FillChangeRequestListGridView";
SqlDataAdapter sqlDa = new SqlDataAdapter(sql,connStr);
DataSet ds = new DataSet();
sqlDa.Fill(ds);
DataView dv = new DataView();

if (ViewState["sortExpr"] != null)
{ dv = new DataView(ds.Tables[0]);
dv.Sort = (string)ViewState["sortExpr"];
}
else
dv = ds.Tables[0].DefaultView;

return dv;
}

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataView dv = new DataView();
dv = bindgrid();
GridView1.DataSource = dv;
GridView1.DataBind();
}
}

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
ViewState["sortExpr"] = e.SortExpression;
GridView1.DataSource = bindgrid();
GridView1.DataBind();
}

protected void GridView1_PageIndexChanging(object sender,
GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource = bindgrid();
GridView1.DataBind();
}

I am a relatively new at all this coming from Access (VBA) to working on
webapps. So far I would say it is a great learning experience and wish I got
into it long ago.

Thanks... John

Doing this

ViewState["sortExpr"] = e.SortExpression;

you keep the column name in the ViewState. You should add another
value to set sorting directrion. For example,

string sort = "Asc";
if(ViewState["sortExpr"] + "" != "" && ViewState["sortExpr"] + "" ==
e.SortExpression)
{
if(ViewState["sortDir"] + "" == "Asc")
sort = "Desc";
}
ViewState["sortExpr"] = e.SortExpression;
ViewState["sortDir"] = sort;

and then

dv.Sort = (string)ViewState["sortExpr"] + " " + ViewState["sortDir"];
 
Alexey, I gave is a quick try and got the following error:  'DataTable must
be set prior to using DataView.'  Below is the code and I added the Dataview
dv line for the dv.Sort.  But the dv.Sort is where the error is occurring.  
Taking baby steps here on this stuff.  So, if you don't mind, explain abit
more about this?
Thanks.
John

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sort = "Asc";
        DataView dv = new DataView();

        if(ViewState["sortExpr"] + "" != "" && ViewState["sortExpr"] + "" ==
e.SortExpression)
        {
            if(ViewState["sortDir"] + "" == "Asc")
                sort = "Desc";
        }
        ViewState["sortExpr"] = e.SortExpression;
        ViewState["sortDir"] = sort;

        dv.Sort = (string)ViewState["sortExpr"] + " " +  ViewState["sortDir"];

        //ViewState["sortExpr"] = e.SortExpression;
        GridView1.DataSource = bindgrid();
        GridView1.DataBind();
    }



Alexey Smirnov said:
Hello.  I have finally gotten the gridview to sort and page.  Butwhat I am
wondering is there a way to reverse the sort from what was originallydone?  
By that, if the user clicked the column header 'Name' it would sort (ASC) by
that column.  If the user clicked on 'Name' again, the column wouldsort DESC
by that column.  I also have an up arrow and down arrow that can beused to
show the column being sorted and in what direction.  Where in code and how
would those be displayed?  Here is the code that I have for the paging and
sorting.  I was keeping it simple but it might be time to turn it up some.
    private DataView bindgrid()
    {          
        string connStr =
ConfigurationManager.ConnectionStrings["ProteusConnectionString"].Connectio­­nString;
        string sql = "SEL_FillChangeRequestListGridView";
        SqlDataAdapter sqlDa = new SqlDataAdapter(sql,connStr);
        DataSet ds = new DataSet();        
        sqlDa.Fill(ds);
        DataView dv = new DataView();
        if (ViewState["sortExpr"] != null)            
        {                       dv = new DataView(ds.Tables[0]);                        
            dv.Sort = (string)ViewState["sortExpr"];          
        }              
        else                    
            dv = ds.Tables[0].DefaultView;      
        return dv;      
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DataView dv = new DataView();
            dv = bindgrid();
            GridView1.DataSource = dv;
            GridView1.DataBind();
        }
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        ViewState["sortExpr"] = e.SortExpression;
        GridView1.DataSource = bindgrid();
        GridView1.DataBind();
    }
    protected void GridView1_PageIndexChanging(object sender,
GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataSource = bindgrid();
        GridView1.DataBind();
    }
I am a relatively new at all this coming from Access (VBA) to workingon
webapps.  So far I would say it is a great learning experience and wish I got
into it long ago.
Thanks... John
Doing this
ViewState["sortExpr"] = e.SortExpression;
you keep the column name in the ViewState. You should add another
value to set sorting directrion. For example,
string sort = "Asc";
if(ViewState["sortExpr"] + "" != "" && ViewState["sortExpr"] + "" ==
e.SortExpression)
{
if(ViewState["sortDir"] + "" == "Asc")
sort = "Desc";
}
ViewState["sortExpr"] = e.SortExpression;
ViewState["sortDir"] = sort;
dv.Sort = (string)ViewState["sortExpr"] + " " +  ViewState["sortDir"];- Hide quoted text -

- Show quoted text -

No, you should add that code with dv.Sort in the bindgrid method. The
error message clearly says that DataView is undefined there

You should have

private DataView bindgrid()
{
string connStr =
ConfigurationManager.ConnectionStrings
["ProteusConnectionString"].Connectio­nString;
string sql = "SEL_FillChangeRequestListGridView";
SqlDataAdapter sqlDa = new SqlDataAdapter(sql,connStr);
DataSet ds = new DataSet();
sqlDa.Fill(ds);
DataView dv = new DataView();


if (ViewState["sortExpr"] != null)
{ dv = new DataView(ds.Tables
[0]);
//dv.Sort = (string)ViewState["sortExpr"];
.....HERE
dv.Sort = (string)ViewState["sortExpr"] + " " + ViewState
["sortDir"];-

}
else
dv = ds.Tables[0].DefaultView;


return dv;
}

it might be also that you would need to do an additional check of
ViewState["sortDir"] (if it's null or not).
 
Alexey, that did it. Thanks for your help on this.
John

Alexey Smirnov said:
Alexey, I gave is a quick try and got the following error: 'DataTable must
be set prior to using DataView.' Below is the code and I added the Dataview
dv line for the dv.Sort. But the dv.Sort is where the error is occurring.
Taking baby steps here on this stuff. So, if you don't mind, explain a bit
more about this?
Thanks.
John

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
string sort = "Asc";
DataView dv = new DataView();

if(ViewState["sortExpr"] + "" != "" && ViewState["sortExpr"] + "" ==
e.SortExpression)
{
if(ViewState["sortDir"] + "" == "Asc")
sort = "Desc";
}
ViewState["sortExpr"] = e.SortExpression;
ViewState["sortDir"] = sort;

dv.Sort = (string)ViewState["sortExpr"] + " " + ViewState["sortDir"];

//ViewState["sortExpr"] = e.SortExpression;
GridView1.DataSource = bindgrid();
GridView1.DataBind();
}



Alexey Smirnov said:
Hello. I have finally gotten the gridview to sort and page. But what I am
wondering is there a way to reverse the sort from what was originally done?
By that, if the user clicked the column header 'Name' it would sort (ASC) by
that column. If the user clicked on 'Name' again, the column would sort DESC
by that column. I also have an up arrow and down arrow that can be used to
show the column being sorted and in what direction. Where in code and how
would those be displayed? Here is the code that I have for the paging and
sorting. I was keeping it simple but it might be time to turn it up some.
private DataView bindgrid()
{
string connStr =
ConfigurationManager.ConnectionStrings["ProteusConnectionString"].Connectio­­nString;
string sql = "SEL_FillChangeRequestListGridView";
SqlDataAdapter sqlDa = new SqlDataAdapter(sql,connStr);
DataSet ds = new DataSet();
sqlDa.Fill(ds);
DataView dv = new DataView();
if (ViewState["sortExpr"] != null)
{ dv = new DataView(ds.Tables[0]);
dv.Sort = (string)ViewState["sortExpr"];
}
else
dv = ds.Tables[0].DefaultView;
return dv;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataView dv = new DataView();
dv = bindgrid();
GridView1.DataSource = dv;
GridView1.DataBind();
}
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
ViewState["sortExpr"] = e.SortExpression;
GridView1.DataSource = bindgrid();
GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender,
GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource = bindgrid();
GridView1.DataBind();
}
I am a relatively new at all this coming from Access (VBA) to working on
webapps. So far I would say it is a great learning experience and wish I got
into it long ago.
Thanks... John
Doing this
ViewState["sortExpr"] = e.SortExpression;
you keep the column name in the ViewState. You should add another
value to set sorting directrion. For example,
string sort = "Asc";
if(ViewState["sortExpr"] + "" != "" && ViewState["sortExpr"] + "" ==
e.SortExpression)
{
if(ViewState["sortDir"] + "" == "Asc")
sort = "Desc";
}
ViewState["sortExpr"] = e.SortExpression;
ViewState["sortDir"] = sort;
dv.Sort = (string)ViewState["sortExpr"] + " " + ViewState["sortDir"];- Hide quoted text -

- Show quoted text -

No, you should add that code with dv.Sort in the bindgrid method. The
error message clearly says that DataView is undefined there

You should have

private DataView bindgrid()
{
string connStr =
ConfigurationManager.ConnectionStrings
["ProteusConnectionString"].Connectio­nString;
string sql = "SEL_FillChangeRequestListGridView";
SqlDataAdapter sqlDa = new SqlDataAdapter(sql,connStr);
DataSet ds = new DataSet();
sqlDa.Fill(ds);
DataView dv = new DataView();


if (ViewState["sortExpr"] != null)
{ dv = new DataView(ds.Tables
[0]);
//dv.Sort = (string)ViewState["sortExpr"];
.....HERE
dv.Sort = (string)ViewState["sortExpr"] + " " + ViewState
["sortDir"];-

}
else
dv = ds.Tables[0].DefaultView;


return dv;
}

it might be also that you would need to do an additional check of
ViewState["sortDir"] (if it's null or not).
 
Alexey, that did it.  Thanks for your help on this.
John



Alexey Smirnov said:
Alexey, I gave is a quick try and got the following error:  'DataTable must
be set prior to using DataView.'  Below is the code and I added theDataview
dv line for the dv.Sort.  But the dv.Sort is where the error is occurring.  
Taking baby steps here on this stuff.  So, if you don't mind, explain a bit
more about this?
Thanks.
John
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sort = "Asc";
        DataView dv = new DataView();
        if(ViewState["sortExpr"] + "" != "" && ViewState["sortExpr"] + "" ==
e.SortExpression)
        {
            if(ViewState["sortDir"] + "" == "Asc")
                sort = "Desc";
        }
        ViewState["sortExpr"] = e.SortExpression;
        ViewState["sortDir"] = sort;
        dv.Sort = (string)ViewState["sortExpr"] + " " +  ViewState["sortDir"];
        //ViewState["sortExpr"] = e.SortExpression;
        GridView1.DataSource = bindgrid();
        GridView1.DataBind();
    }
:
Hello.  I have finally gotten the gridview to sort and page.  But what I am
wondering is there a way to reverse the sort from what was originally done?  
By that, if the user clicked the column header 'Name' it would sort (ASC) by
that column.  If the user clicked on 'Name' again, the column would sort DESC
by that column.  I also have an up arrow and down arrow that can be used to
show the column being sorted and in what direction.  Where in code and how
would those be displayed?  Here is the code that I have for thepaging and
sorting.  I was keeping it simple but it might be time to turn it up some.
    private DataView bindgrid()
    {          
        string connStr =
ConfigurationManager.ConnectionStrings["ProteusConnectionString"]..Connectio­­­nString;
        string sql = "SEL_FillChangeRequestListGridView";
        SqlDataAdapter sqlDa = new SqlDataAdapter(sql,connStr);
        DataSet ds = new DataSet();        
        sqlDa.Fill(ds);
        DataView dv = new DataView();
        if (ViewState["sortExpr"] != null)            
        {                       dv = new DataView(ds.Tables[0]);                       
            dv.Sort = (string)ViewState["sortExpr"];          
        }              
        else                    
            dv = ds.Tables[0].DefaultView;      
        return dv;      
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DataView dv = new DataView();
            dv = bindgrid();
            GridView1.DataSource = dv;
            GridView1.DataBind();
        }
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        ViewState["sortExpr"] = e.SortExpression;
        GridView1.DataSource = bindgrid();
        GridView1.DataBind();
    }
    protected void GridView1_PageIndexChanging(object sender,
GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataSource = bindgrid();
        GridView1.DataBind();
    }
I am a relatively new at all this coming from Access (VBA) to working on
webapps.  So far I would say it is a great learning experience and wish I got
into it long ago.
Thanks... John
Doing this
ViewState["sortExpr"] = e.SortExpression;
you keep the column name in the ViewState. You should add another
value to set sorting directrion. For example,
string sort = "Asc";
if(ViewState["sortExpr"] + "" != "" && ViewState["sortExpr"] + ""==
e.SortExpression)
{
if(ViewState["sortDir"] + "" == "Asc")
sort = "Desc";
}
ViewState["sortExpr"] = e.SortExpression;
ViewState["sortDir"] = sort;
and then
dv.Sort = (string)ViewState["sortExpr"] + " " +  ViewState["sortDir"];- Hide quoted text -
- Show quoted text -
No, you should add that code with dv.Sort in the bindgrid method. The
error message clearly says that DataView is undefined there
You should have
private DataView bindgrid()
    {
        string connStr =
ConfigurationManager.ConnectionStrings
["ProteusConnectionString"].Connectio­nString;
        string sql = "SEL_FillChangeRequestListGridView";
        SqlDataAdapter sqlDa = new SqlDataAdapter(sql,connStr);
        DataSet ds = new DataSet();
        sqlDa.Fill(ds);
        DataView dv = new DataView();
        if (ViewState["sortExpr"] != null)
        {                       dv = new DataView(ds.Tables
[0]);
            //dv.Sort = (string)ViewState["sortExpr"];
.....HERE
dv.Sort = (string)ViewState["sortExpr"] + " " +  ViewState
["sortDir"];-
        }
        else
            dv = ds.Tables[0].DefaultView;
        return dv;
    }
it might be also that you would need to do an additional check of
ViewState["sortDir"] (if it's null or not).- Hide quoted text -

- Show quoted text -

Great, glad it works now for you
 
Back
Top