private const string ASCENDING = " ASC"; private const string DESCENDING = " DESC"; public SortDirection GridViewSortDirection { get { if (ViewState["sortDirection"] == null) ViewState["sortDirection"] = SortDirection.Ascending; return (SortDirection)ViewState["sortDirection"]; } set { ViewState["sortDirection"] = value; } } protected void GridView2_Sorting(object sender, GridViewSortEventArgs e) { string sortExpression = e.SortExpression; if (GridViewSortDirection == SortDirection.Ascending) { GridViewSortDirection = SortDirection.Descending; SortGridView1(sortExpression, DESCENDING); } else { GridViewSortDirection = SortDirection.Ascending; SortGridView1(sortExpression, ASCENDING); } } private void SortGridView1(string sortExpression, string direction) { DataTable dt = SearchTable().Tables[0]; DataView dv = new DataView(dt); dv.Sort = sortExpression + direction; GridView2.DataSource = dv; GridView2.DataBind(); }
public DataSet SearchTable() { string sql1 = "SELECT * from dbo.Documents1"; bool flag = false; if (!txtRef.Text.Equals("")) { if (flag == false) { sql1 = sql1 + " where Ref LIKE N'%" + txtRef.Text + "%'"; flag = true; } else { sql1 = sql1 + " and Ref LIKE N'%" + txtRef.Text + "%'"; } } if (!txtSubject.Text.Equals("")) { if (flag == false) { sql1 = sql1 + " where Subject LIKE N'%" + txtSubject.Text + "%'"; flag = true; } else { sql1 = sql1 + " and Subject LIKE N'%" + txtSubject.Text + "%'"; } } using (SqlConnection con = new SqlConnection("Data Source=MEHDI-PC\\SQLEXPRESS;Initial Catalog=PIMS;Integrated Security=True")) { using (SqlCommand cmd = new SqlCommand()) { cmd.Connection = con; cmd.CommandText = sql1 + ";"; //cmd.CommandType = CommandType.StoredProcedure; con.Open(); //dataset object to get all select statement results DataSet ds = new DataSet(); //sql dataadoptor to fill dataset using (SqlDataAdapter adp = new SqlDataAdapter(cmd)) { adp.Fill(ds); } if (con.State == ConnectionState.Open) { con.Close(); } return ds; } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)