GridView多条件筛选数据

最近有一个需求,需要对gridview中的数据进行多条件塞选,比较粗糙的解决办法做了一个demo.

下面截取的部分代码中 toolName,lcs, purpose, version是根据这些数据对gridview进行塞选。

在代码中注意用linq时判断两个数是否相等最好是用”==”,如果用equals方法需小心,如果A.equals(B),A为空的话不会出现任何错误,只能调试才能发现错,因为linq里面的lamda表达式内部的确抛了一个错误,但是不会将此错误抛向界面。


private void Filter() {
            string toolName = txtToolname.Text;
            string lcs = ddlLifeCycle.SelectedValue;
            string purpose = ddlPurpose.SelectedValue;
            string version = ddlVersion.SelectedValue;
           DataSet ds = ServiceIPM.GetTools(Infosys.AMW.Common.AppConstants.LoginName, Infosys.AMW.Common.AppConstants.ProjectCodeFromURL, Infosys.AMW.Common.AppConstants.TLPITEMTYPE);
            //DataSet ds = GridViewExportUtil.GetToolsData();
            DataTable dt = ds.Tables[0];
            EnumerableRowCollection query=null;
            if (IsDateSetNullOREmpty(ds))
            {
                if (!string.IsNullOrEmpty(toolName) && lcs != "select" && purpose != "select" && version != "select")
                {
                    query = from data in dt.AsEnumerable()
                            where
                            data.Field("m__strToolName").ToUpper().Contains(toolName.ToUpper())
                            && data.Field("m__strLCStage") == lcs
                            && data.Field("m__strVersion") == version
                            && data.Field("m__strPurpose") == purpose
                            select data;
                }
                else if (string.IsNullOrEmpty(toolName))
                {
                    query = from data in dt.AsEnumerable()
                            where
                              data.Field("m__strLCStage") == (lcs)
                                || data.Field("m__strVersion") == version
                                 || data.Field("m__strPurpose") == purpose
                            select data;
                }
                else
                {
                    query = from data in dt.AsEnumerable()
                            where
                            (data.Field("m__strToolName").ToUpper().Contains(string.IsNullOrEmpty(toolName) ? null : (toolName.ToUpper()))
                            || data.Field("m__strLCStage") == (lcs)
                            || data.Field("m__strVersion") == version
                            || data.Field("m__strPurpose") == purpose)
                            select data;
                }
                //return query == null ? null : query.AsDataView();
               
            }
            if (query != null)
            {
                BindDataViewToGV(query.AsDataView());
            }
            else { BindDataViewToGV(null); }
            //return null;
        }
        private void BindDataViewToGV(DataView dv) {
            if (dv != null && dv.Count > 0)
            {
                string sortExpression = gvPlanedTools.Attributes["SortExpression"];
                string sortDirection = gvPlanedTools.Attributes["SortDirection"];
                ViewState["PageCount"] = dv.Count.ToString();
                if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
                {
                    dv.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
                }
                gvPlanedTools.PageSize = Convert.ToInt32(ControlDrp);
                gvPlanedTools.DataSource = dv;
                gvPlanedTools.DataBind();
                InitRecords();
                SortForImage(gvPlanedTools.Attributes["SortDirection"], gvPlanedTools.Attributes["SortExpression"]);
            }
            else {
                panShow.Visible = false;
                panError.Visible = true;
                lbError.Text = PageMessage.ERRORWEBSERVICEDATA;
            }
            //ViewState["PageCount"] = dv.Count.ToString();
            //gvPlanedTools.DataSource = dv;
            //gvPlanedTools.DataBind();
            //InitRecords();
        }


你可能感兴趣的:(asp.net)