J
JohnE
I have a webpage with a gridview on it that has 6 pages with 20 per page.
The issue I have is when it sorts and then try and go to another page the
sorting discontinues and the grid order goes back to the original order. I
am not getting both to work together and it is getting frustrating. I am
definitely considered a newbie at this, but I'm working to better the status.
Here is all the C# code I have concerning the gridview (no html, that is
fine).
protected void Page_Load(object sender, EventArgs e)
{
string connStr =
ConfigurationManager.ConnectionStrings["ProteusConnectionString"].ConnectionString;
DataTable dt = new DataTable();
SqlConnection conn = new SqlConnection(connStr);
{
conn.Open();
string sql = "FillGridView";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter sqlDa = new SqlDataAdapter(cmd);
sqlDa.Fill(dt);
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
else
{
//Load data and do nothing
}
}
{
conn.Close();
}
}
protected void GridView1_PageIndexChanging(object sender,
GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
private string ConvertSortDirectionToSQL(SortDirection sortDirection)
{
string newSortDirection = string.Empty;
switch (sortDirection)
{
case SortDirection.Ascending:
newSortDirection = "ASC";
break;
case SortDirection.Descending:
newSortDirection = "DESC";
break;
}
return newSortDirection;
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dt = GridView1.DataSource as DataTable;
if (dt != null)
{
DataView dvw = new DataView(dt);
dvw.Sort = e.SortExpression + " " +
ConvertSortDirectionToSQL(e.SortDirection);
e.SortExpression = e.SortExpression.ToString();
GridView1.DataSource = dvw;
GridView1.DataBind();
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs
e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataBind();
}
protected void GridView1_RowCancelingEdit(object sender,
GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GridView1.DataBind();
}
protected void GridView1_SelectedIndexChanging(object sender,
GridViewSelectEventArgs e)
{
GridViewRow row = GridView1.Rows[e.NewSelectedIndex];
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
}
}
Can anyone please review this and see what I am doing wrong or its right but
just not enough. Whatever the case, let me know.
Thanks... John
The issue I have is when it sorts and then try and go to another page the
sorting discontinues and the grid order goes back to the original order. I
am not getting both to work together and it is getting frustrating. I am
definitely considered a newbie at this, but I'm working to better the status.
Here is all the C# code I have concerning the gridview (no html, that is
fine).
protected void Page_Load(object sender, EventArgs e)
{
string connStr =
ConfigurationManager.ConnectionStrings["ProteusConnectionString"].ConnectionString;
DataTable dt = new DataTable();
SqlConnection conn = new SqlConnection(connStr);
{
conn.Open();
string sql = "FillGridView";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter sqlDa = new SqlDataAdapter(cmd);
sqlDa.Fill(dt);
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
else
{
//Load data and do nothing
}
}
{
conn.Close();
}
}
protected void GridView1_PageIndexChanging(object sender,
GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
private string ConvertSortDirectionToSQL(SortDirection sortDirection)
{
string newSortDirection = string.Empty;
switch (sortDirection)
{
case SortDirection.Ascending:
newSortDirection = "ASC";
break;
case SortDirection.Descending:
newSortDirection = "DESC";
break;
}
return newSortDirection;
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dt = GridView1.DataSource as DataTable;
if (dt != null)
{
DataView dvw = new DataView(dt);
dvw.Sort = e.SortExpression + " " +
ConvertSortDirectionToSQL(e.SortDirection);
e.SortExpression = e.SortExpression.ToString();
GridView1.DataSource = dvw;
GridView1.DataBind();
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs
e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataBind();
}
protected void GridView1_RowCancelingEdit(object sender,
GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GridView1.DataBind();
}
protected void GridView1_SelectedIndexChanging(object sender,
GridViewSelectEventArgs e)
{
GridViewRow row = GridView1.Rows[e.NewSelectedIndex];
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
}
}
Can anyone please review this and see what I am doing wrong or its right but
just not enough. Whatever the case, let me know.
Thanks... John