Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Postback problem with DataGrid

Hi Everyone,

I have an odd problem that I can't figure out (hence the post :) ).

I have a gridview with manual columns and sorting. All the columns sort fine. What I have noticed is that the first column, Job Title, does not sort on the first click but does on subsequent clicks. If I sort Job Title 5 or 6 times and then another column, that column doesn't sort the first time around either. However, if I sort any other column the first time around (without touching Job Title), it works fine. Once I hit Job Title, it gets messed up.

Here is my gridview code and code behind for all the sorting and everything.

Thanks!

<form runat="server">
<p>
<asp:GridView AutoGenerateColumns="false" BorderColor="000080"
ID="gv_Careers" HeaderStyle-BackColor="#cccccc" AllowSorting="true"
HeaderStyle-HorizontalAlign="Center" FooterStyle-BackColor="#cccccc"
Width="75%" ShowFooter="false" OnSorting="gv_Careers_Sorting" RowStyle-HorizontalAlign="Center" runat="server">
<Columns>
<asp:HyperLinkField SortExpression="JobTitle" HeaderText="JobTitle" DataTextField="Job Title" DataNavigateUrlFields="JobID" DataNavigateUrlFormatString="~/content/default.aspx?pud=d1ac4030-33e8-4574-a4a1-46247162eef4&tbbd=1b779fd5-36eb-49b7-afdb-2bde1085226e&jobid={0}" />
<asp:BoundField SortExpression="Category" HeaderText="Category" DataField="Category" />
<asp:BoundField SortExpression="City" HeaderText="City" DataField="City" />
<asp:BoundField SortExpression="State" HeaderText="State" DataField="State" />
</Columns>
</asp:GridView>
</p>
</form>

Codebehind

ublic partial class Base_CMSCareersPage : System.Web.UI.MasterPage
{
private const string ASCENDING = " ASC";
private const string DESCENDING = " DESC";

private DataSet GetData()
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString);
SqlCommand command = new SqlCommand("Select * from CareersSample", conn);
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet ds = new DataSet();
adapter.Fill(ds, "CareersSample");

this.gv_Careers.DataSource = ds;
this.gv_Careers.DataMember = "CareersSample";
this.gv_Careers.DataBind();

return ds;

}

protected void Page_Load(object sender, EventArgs e)
{

if (!Page.IsPostBack)
{
if (this.RightSideBarAggregate1.Visible == false)
{
this.rtCol.Visible = false;
this.rtBottomCol.Visible = false;
this.midCol.Attributes["style"] = "width:576px;";
}
}

GetData();
}



public SortDirection GridViewSortDirection
{
get
{
if (ViewState["sortDirection"] == null)
ViewState["sortDirection"] = SortDirection.Ascending;
return (SortDirection) ViewState["sortDirection"];
}
set
{
ViewState["sortDirection"] = value;
}
}

protected void gv_Careers_Sorting(object sender, GridViewSortEventArgs e)
{
string sortExpression = e.SortExpression;

if (GridViewSortDirection == SortDirection.Ascending)
{
GridViewSortDirection = SortDirection.Descending;
SortGridView(sortExpression, DESCENDING);
}
else
{
GridViewSortDirection = SortDirection.Ascending;
SortGridView(sortExpression, ASCENDING);
}
}

private void SortGridView(string sortExpression,string direction)
{
DataTable dt = GetData().Tables[ 0];
DataView dv = new DataView(dt);
dv.Sort = sortExpression + direction;

gv_Careers.DataSource = dv;
gv_Careers.DataBind();
}
}
[4639 byte] By [goodfella] at [2007-11-11 10:30:48]
# 1 Re: Postback problem with DataGrid
I've narrowed this problem down to an ascending/descending issue. Columns that are ascending sort fine initially. descending columns are the problem. can't find anything in the code to make this work right.
goodfella at 2007-11-11 23:11:51 >