다음 함수를 구현하여 모든 레코드를 성공적으로 정렬하고 있습니다.검색된 레코드를 gridview에서 정렬하는 방법
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;
}
}
}
검색된 레코드를 정렬하는 데 문제가 있습니다. 나는 단지 필요 동안 코드 위
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;
}
}
}
이 Gridview2의 검색 기록을 정렬되어 있지만 레코드를 결합 할 때, 그것은 ... 데이터베이스 테이블에서 모든 레코드를 구속력이 : 내가 신청하는 코드는 다음과 같습니다 검색된 레코드를 바인드하여 표시합니다. 내가 어디로 잘못 가고 있는지 이해할 수 없다. 어떤 도움을 많이 주시면 감사하겠습니다. 미리 감사드립니다.
SearchTable() 함수는 어디에 있습니까? 이 함수 코드를 추가 할 수 있습니까? –
@SainPradeep 신속한 답장을 보내 주셔서 감사합니다. 귀하의 검토를 위해 SearchTable()을 추가했습니다. 고맙습니다. –
디버그하여 조건에 맞는 최종 검색어를 확인하십시오. –