How can I sort data wh-ich is in a datagrid view.
The language I am using is C sharp language.
Regards.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Venkat KumarPosted Apr 25, 2015, 2:23 AM
sort.aspx.cs
public partial class sort : System.Web.UI.Page
{
string conn = ConfigurationManager.ConnectionStrings["con"].ToString();
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
public void BindData()
{
SqlConnection connec = new SqlConnection();
connec.ConnectionString = conn;
connec.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = connec;
cmd.CommandText = "select * from tbldept";
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(ds);
gv.DataSource = ds;
gv.DataBind();
connec.Close();
}
public SortDirection dir
{
get
{
if (ViewState["dirState"] == null)
{
ViewState["dirState"] = SortDirection.Ascending;
}
return (SortDirection)ViewState["dirState"];
}
set
{
ViewState["dirState"] = value;
}
}
protected void gv_Sorting(object sender, GridViewSortEventArgs e)
{
BindData();
DataTable dt = new DataTable();
dt = ds.Tables[0];
{
string SortDir = string.Empty;
if (dir == SortDirection.Ascending)
{
dir = SortDirection.Descending;
SortDir = "Desc";
}
else
{
dir = SortDirection.Ascending;
SortDir = "Asc";
}
DataView sortedView = new DataView(dt);
sortedView.Sort = e.SortExpression + " " + SortDir;
gv.DataSource = sortedView;
gv.DataBind();
}
}
}
sort.aspx
Khargesh RajputPosted Apr 24, 2015, 11:48 PM
then in your query use
order by clause