Friday, October 30, 2015

search datatable using viewstate without go to database every postback and search using dataview filter option example using c#

first time you should fill viewstate with your database table.
 private void LoadData()
    {
        try
        {
            DataTable TblSections = new DataTable();

            if (ViewState["Pusers"] == null)
            {
                if (this.currentRequest.REQUEST_ID != null)
                {
                    TblSections = workflowGetData.WORK_FLOWS_PERMISSION_MANAGERIAL_GET_STAGE_USERS(this.currentRequest.WORK_FLOW_ID.ToString(), this.currentRequest.WORK_FLOW_TYPE.ToString(), this.currentRequest.CURRENT_STAGE_IDs[0].ToString(), "");
                    ViewState["Pusers"] = TblSections;
                    //else
                    //    TblSections = workflowGetData.WORK_FLOWS_PERMISSION_MANAGERIAL_GET_STAGE_USERS_BY_REQUEST_ID(hid_workflow_id.Value, hid_workflow_type.Value, hid_stage_id.Value, "", hid_request_id.Value);
                }
            }
            else
                TblSections = (DataTable)ViewState["Pusers"];

            DataView DV = new DataView();
            DV = TblSections.DefaultView;
            if (TXT_USER_ID.Text.Length > 0)
                DV.RowFilter = "USER_ID=" + TXT_USER_ID.Text;
            if (TXT_USER_FULL_NAME.Text.Length > 0)
                DV.RowFilter = String.Format("emp_name LIKE '*{0}*'", EscapeLikeValue(TXT_USER_FULL_NAME.Text));

            GV_USERS.DataSource = TblSections;
            GV_USERS.DataBind();
        }
        catch (Exception ex)
        {
            basepage.FormShowMessage(lblmessage, Enum_Class.MessageStatus.failure, Resources.GlobalErrorMessages.Error_GeneralException);
        }
    }
---------------------------------------------
  public static string EscapeLikeValue(string valueWithoutWildcards)
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < valueWithoutWildcards.Length; i++)
        {
            char c = valueWithoutWildcards[i];
            if (c == '*' || c == '%' || c == '[' || c == ']')
                sb.Append("[").Append(c).Append("]");
            else if (c == '\'')
                sb.Append("''");
            else
                sb.Append(c);
        }
        return sb.ToString();
    } 

No comments:

Post a Comment