Sorting in ASP.NET Gridview

5 views
Skip to first unread message

Meet

unread,
Jan 18, 2011, 2:18:58 AM1/18/11
to DotNetProfessionals-Group
SORTING IN ASP.NET GRIDVIEW CONTROL

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class viewgamestatus : System.Web.UI.Page
{
public string getgames =
ConfigurationManager.ConnectionStrings["str"].ToString();
DataSet dsinbox = new DataSet();
DataView dv = new DataView();

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dv= bindgrid();
GridView1.DataSource = dv;
GridView1.DataBind();
}
}
********************/ FUNCTION TO BIND THE GRIDVIEW /
***********************
private DataView bindgrid()
{
try
{
SqlConnection coninbox = new SqlConnection(getgames);
SqlDataAdapter dainbox = new SqlDataAdapter("select
Sr_No,status,gamename,gameresult from games where SID='" +
Session["childid"].ToString() + "'and school_id='" +
Session["schoolid"].ToString() + "'", coninbox);
dainbox.Fill(dsinbox);
if (ViewState["sortExpr"] != null)
{
dv = new DataView(dsinbox.Tables[0]);
dv.Sort =
(string)ViewState["sortExpr"]; }
else
dv = dsinbox.Tables[0].DefaultView;
return dv;
}
catch (Exception ex)
{
return dv;
Response.Redirect("error.aspx");
// Response.Write(ex.Message);
}
}
protected void GridView1_SelectedIndexChanged(object sender,
EventArgs e)
{
panelassignment.Visible = true;
try
{
SqlConnection condownload = new SqlConnection(getgames);
SqlDataAdapter da = new SqlDataAdapter("select gameresult
from games where Sr_no='" + GridView1.SelectedRow.Cells[1].Text +
"'and school_id='" + Session["schoolid"].ToString() + "'",
condownload);
DataSet ds = new DataSet();
da.Fill(ds);
DataList1.DataSource = ds;
DataList1.DataBind();
Page.ClientScript.RegisterStartupScript(this.GetType(),
"alert", "initbox();", true);
condownload.Close();
condownload.Open();
SqlCommand conupdate = new SqlCommand("update games set
status='Read' where Sr_No='" + GridView1.SelectedRow.Cells[1].Text +
"'", condownload);
conupdate.ExecuteNonQuery();
condownload.Close();
bindgrid();
foreach (GridViewRow row in GridView1.Rows)
{ row.Cells[0].ToolTip = "Click to view details"; }
}
catch (Exception ex)
{
Response.Redirect("error.aspx");
}
}
protected void GridView1_PageIndexChanging(object sender,
GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource= bindgrid();
GridView1.DataBind();
}
protected void GridView1_Sorting(object sender,
GridViewSortEventArgs e)
{
ViewState["sortExpr"] = e.SortExpression;
/***** SORTEXPRESSION VALUE WILL BE GIVEN ON THE TIME OF DATA BINDING
WHEN WE USE BOUND FIELDS IN GRIDVIEW. IT IS THE VALUE ON THE BASIS OF
WHICH WE WANT TO SORT THE ITEMS [ IN MY CASE I MSORTING ON THE BASIS
OF STATUS OF A NEW MESSAGE]*******/

GridView1.DataSource = bindgrid();
GridView1.DataBind();
}
}
Reply all
Reply to author
Forward
0 new messages