I have a GridView and a datasource. The only properties i set to datasource are the ConnectionStrings, ProviderName, runat and ID as shown below. And according to a criteria i set
SelectCommand property of my datasource on my .cs file of .aspx on which exists my gidview.
My problem is that; although i set AllowPaging and AllowSorting properties of my gridview to true, it nor sorts or pages the gridview but just disappears. I just wonder how i could get over this?
kem06.net
Member
56 Points
306 Posts
How to sort and page gridview
Jan 21, 2008 06:59 AM|LINK
I have a GridView and a datasource. The only properties i set to datasource are the ConnectionStrings, ProviderName, runat and ID as shown below. And according to a criteria i set SelectCommand property of my datasource on my .cs file of .aspx on which exists my gidview. My problem is that; although i set AllowPaging and AllowSorting properties of my gridview to true, it nor sorts or pages the gridview but just disappears. I just wonder how i could get over this?
<asp:SqlDataSource ID="DSourceSirketSec" runat="server" ConnectionString="<%$ ConnectionStrings:Constring %>" ProviderName="<%$ ConnectionStrings:Constring .ProviderName %>"> </asp:SqlDataSource>Thanks...
how to sort page gridview datasource
vik20000in
All-Star
25882 Points
3993 Posts
MVP
Re: How to sort and page gridview
Jan 21, 2008 07:22 AM|LINK
are u setting the select command in the page load inside if(!Ispostback) condition. if yes than that is the problem
www.vikramlakhotia.com
Please mark the answer if it helped you
kem06.net
Member
56 Points
306 Posts
Re: How to sort and page gridview
Jan 21, 2008 07:54 AM|LINK
No actually, i set the selectcommand in click event of a button.
hspharic
Member
649 Points
119 Posts
Re: How to sort and page gridview
Jan 21, 2008 09:18 AM|LINK
Hi,
Try this,
In aspx file,
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>" ProviderName="<%$ ConnectionStrings:pubsConnectionString.providerName %>" ></asp:SqlDataSource> <asp:Button ID="Button1" runat="server" OnClick="BtnSelect_Click" Text="Button" /><br /> <asp:GridView ID="GridView1" runat="server" AllowPaging="true" AllowSorting="true" OnPageIndexChanging="GridView1_PageIndexChanging" OnSorting="GridView1_Sorting"> </asp:GridView>In code behind,
protected void BtnSelect_Click(object sender, EventArgs e){
BindGrid();
}
private void BindGrid(){
string sort = ""; if (ViewState["sortExpr"] + "" != "") sort = "order by " + ViewState["sortExpr"] + " " + ViewState["sortDir"]; SqlDataSource1.SelectCommand = "select * from authors " + sort;GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e){
string sort = "Asc";if(ViewState["sortExpr"] + "" != "" && ViewState["sortExpr"] + "" == e.SortExpression){
if(ViewState["sortDir"] + "" == "Asc") sort = "Desc";}
ViewState["sortExpr"] = e.SortExpression;ViewState["sortDir"] = sort;BindGrid();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e){
GridView1.PageIndex = e.NewPageIndex;
BindGrid();
}
Click Answered when you satisfied my replies.
vinz
All-Star
126926 Points
17922 Posts
MVP
Re: How to sort and page gridview
Jan 21, 2008 11:44 AM|LINK
Check this
http://quickstarts.asp.net/QuickStartv20/util/srcview.aspx?path=~/aspnet/samples/data/GridViewSorting.src
MessageBox Controls for WebForms | Blog | Twitter | Linkedin
kem06.net
Member
56 Points
306 Posts
Re: How to sort and page gridview
Jan 22, 2008 07:06 AM|LINK
Thanks hspharic, it works fine...