I am sorting all records successfully by implementing the following functions.
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 GridView1_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)
{
// You can cache the DataTable for improving performance
DataTable dt = GetData().Tables[0];
DataView dv = new DataView(dt);
dv.Sort = sortExpression + direction;
GridView1.DataSource = dv;
GridView1.DataBind();
}
public DataSet GetData()
{
SqlConnection con = new SqlConnection("Data Source=MEHDI-PC\\SQLEXPRESS; Initial Catalog=PIMS; Integrated Security=true;");
{
using (SqlCommand cmd = new SqlCommand())
{
String sql = "select * from dbo.Documents1";
cmd.Connection = con;
cmd.CommandText = sql;
con.Open();
DataSet ds = new DataSet();
using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
{
adp.Fill(ds);
}
return ds;
}
}
}
I am having problem sorting the searched records. Code i am applying is as following :
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 + "%'";
}
}
if (!ddlSource.Text.Equals(""))
{
if (flag == false)
{
sql1 = sql1 + " where Src =N'" + ddlSource.Text + "'";
flag = true;
}
else
{
sql1 = sql1 + " and Src =N'" + ddlSource.Text + "'";
}
}
if (!ddlDestination.Text.Equals(""))
{
if (flag == false)
{
sql1 = sql1 + " where Dst=N'" + ddlDestination.Text + "'";
flag = true;
}
else
{
sql1 = sql1 + " and Dst =N'" + ddlDestination.Text + "'";
}
}
if (!ddlMedium.Text.Equals(""))
{
if (flag == false)
{
sql1 = sql1 + " where Medium =N'" + ddlMedium.Text + "'";
flag = true;
}
else
{
sql1 = sql1 + " and Medium =N'" + ddlMedium.Text + "'";
}
}
if (!txtDatePrinted.Text.Equals(""))
{
if (flag == false)
{
sql1 = sql1 + " where Date_Printed =N'" + txtDatePrinted.Text + "'";
flag = true;
}
else
{
sql1 = sql1 + " and Date_Printed =N'" + txtDatePrinted.Text + "'";
}
}
if (!txtDateReceived.Text.Equals(""))
{
if (flag == false)
{
sql1 = sql1 + " where Date_Received =N'" + txtDateReceived.Text + "'";
flag = true;
}
else
{
sql1 = sql1 + " and Date_Received =N'" + txtDateReceived.Text + "'";
}
}
if (!ddlDocumentType.Text.Equals(""))
{
if (flag == false)
{
sql1 = sql1 + " where Document_Type =N'" + ddlDocumentType.Text + "'";
flag = true;
}
else
{
sql1 = sql1 + " and Document_Type =N'" + ddlDocumentType.Text + "'";
}
}
if (!txtDueDate.Text.Equals(""))
{
if (flag == false)
{
sql1 = sql1 + " where Due_Date = N'" + txtDueDate.Text + "'";
flag = true;
}
else
{
sql1 = sql1 + " and Due_Date =N'" + txtDueDate.Text + "'";
}
}
if (!txtActualDate.Text.Equals(""))
{
if (flag == false)
{
sql1 = sql1 + " where Actual_Date= N'" + txtActualDate.Text + "'";
flag = true;
}
else
{
sql1 = sql1 + " and Actual_Date=N'" + txtActualDate.Text + "'";
}
}
if (!txtContent.Text.Equals(""))
{
if (flag == false)
{
sql1 = sql1 + " where Content=N'" + txtContent.Text + "'";
flag = true;
}
else
{
sql1 = sql1 + " and Content=N'" + txtContent.Text + "'";
}
}
if (!txtTag.Text.Equals(""))
{
if (flag == false)
{
sql1 = sql1 + " where Tag =N'" + txtTag.Text + "'";
flag = true;
}
else
{
sql1 = sql1 + " and Tag =N'" + txtTag.Text + "'";
}
}
if (!txtIssue.Text.Equals(""))
{
if (flag == false)
{
sql1 = sql1 + " where Issue_No = N'" + txtIssue.Text + "'";
flag = true;
}
else
{
sql1 = sql1 + " and Issue_No = N'" + txtIssue.Text + "'";
}
}
if (!txtNotes.Text.Equals(""))
{
if (flag == false)
{
sql1 = sql1 + " where Notes = N'" + txtNotes.Text + "'";
flag = true;
}
else
{
sql1 = sql1 + " and Notes = N'" + txtNotes.Text + "'";
}
}
if (!ddlAssignedTo.Text.Equals(""))
{
if (flag == false)
{
sql1 = sql1 + " where Assigned_To = N'" + ddlAssignedTo.Text + "'";
flag = true;
}
else
{
sql1 = sql1 + " and Assigned_To = N'" + ddlAssignedTo.Text + "'";
}
}
if (!txtReplyRef.Text.Equals(""))
{
if (flag == false)
{
sql1 = sql1 + " where Reply_Ref LIKE N'%" + txtReplyRef.Text + "%'";
flag = true;
}
else
{
sql1 = sql1 + " and Reply_Ref LIKE N'%" + txtReplyRef.Text + "%'";
}
}
if (!ddlPriority.Text.Equals(""))
{
if (flag == false)
{
sql1 = sql1 + " where Priority = N'" + ddlPriority.Text + "'";
flag = true;
}
else
{
sql1 = sql1 + " and Priority = N'" + ddlPriority.Text + "'";
}
}
if (!ddlStatus.Text.Equals(""))
{
if (flag == false)
{
sql1 = sql1 + " where Status LIKE N'%" + ddlStatus.Text + "%'";
flag = true;
}
else
{
sql1 = sql1 + " and Status LIKE N'%" + ddlStatus.Text + "%'";
}
}
if (!ddlResponse.Text.Equals(""))
{
if (flag == false)
{
sql1 = sql1 + " where Response LIKE N'%" + ddlResponse.Text + "%'";
flag = true;
}
else
{
sql1 = sql1 + " and Response LIKE N'%" + ddlResponse.Text + "%'";
}
}
if (!txtPhysicalFileNo.Text.Equals(""))
{
if (flag == false)
{
sql1 = sql1 + " where Physical_File_No LIKE N'%" + txtPhysicalFileNo.Text + "%'";
flag = true;
}
else
{
sql1 = sql1 + " and Physical_File_No LIKE N'%" + txtPhysicalFileNo.Text + "%'";
}
}
if (!txtPhysicalRackLocation.Text.Equals(""))
{
if (flag == false)
{
sql1 = sql1 + " where Physical_Rack_Location LIKE N'%" + txtPhysicalRackLocation.Text + "%'";
flag = true;
}
else
{
sql1 = sql1 + " and Physical_Rack_Location LIKE N'%" + txtPhysicalRackLocation.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;
}
}
}
Above code is sorting the searched records in Gridview2, but when it binds the records, it is binding all the records from the database table ... while I need only the searched records to be binded and shown. I cant understand where I am going wrong. Any help will be much appreciated. Thanks in advance.
Please change your searchTable function query like this.
string sql1 = "SELECT * from dbo.Documents1 where 1=1";
bool flag = false;
if (!txtRef.Text.Equals(""))
{
if (flag == false)
{
sql1 = sql1 + " and Ref LIKE N'%" + txtRef.Text + "%'";
flag = true;
}
else
{
sql1 = sql1 + " and Ref LIKE N'%" + txtRef.Text + "%'";
}
}
You need not to put "where" in every if condition you need to add condition with "and" keyword.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.