1

例如,我有 4textbox和 4 comboBox,我想DatagridView使用其中输入的值进行搜索,并且textboxes不想使用空值进行搜索。我可以使用 if Condition 来完成这项工作,但我认为它没有经过优化并且非常复杂。comboBoxes

4

1 回答 1

1

页面加载时从 sql server 获取数据到静态列表。if(combox1.checked){ list.Where(obj=>obj.<field name for that combobox>)然后在过滤器按钮单击事件中,逐步添加 8 个 if 语句,如 wise 过滤器。以下是示例代码。我想你可能明白这一点。

static List<CRMIssueHeader> issueheader=new List<CRMIssueHeader>();

    protected void Page_Load(object sender, EventArgs e)
    {
        btnUpdate.Visible = false;
        if (!Page.IsPostBack)
        { 
            issueheader = _CRMService.CRMTRN02_GetIssueHeader();
        }
    }

protected void btnOK_Click(object sender, EventArgs e)
{
  List<CRMIssueHeader> IHeader = new List<CRMIssueHeader>();
        IHeader = issueheader;
       //_CRMService = new CRMService();
        //issueheader  = _CRMService.CRMTRN02_GetIssueHeader();
        if (!chknew.Checked)
            IHeader = IHeader.Where(obj => obj.IssueStatus != 0).ToList();
        if (!chkWIP.Checked)
            IHeader = IHeader.Where(obj => obj.IssueStatus != 1).ToList();
        if (!chkFixed.Checked)
            IHeader = IHeader.Where(obj => obj.IssueStatus != 2).ToList();
        if (chkAccepted.Checked)
            IHeader = IHeader.Where(obj => obj.IssueStatus != 3).ToList();
        if (chkClarification.Checked)
            IHeader = IHeader.Where(obj => obj.IssueStatus != 4).ToList();
        if (chkCancel.Checked)
            IHeader = IHeader.Where(obj => obj.IssueStatus != 5).ToList();
        if (chkOnHold.Checked)
            IHeader = IHeader.Where(obj => obj.IssueStatus != 6).ToList();
        if (ddlCustomers.SelectedValue.ToString() != "All")
            IHeader = IHeader.Where(obj => obj.CustomerCode == ddlCustomers.SelectedValue.ToString()).ToList();
        if (txtFrom.Text.Length != 0 && txtTo.Text.Length != 0)
            IHeader = IHeader.Where(obj => obj.ReportedOn >= DateTime.Parse(txtFrom.Text) && obj.ReportedOn <= DateTime.Parse(txtTo.Text)).ToList();
        else if (txtFrom.Text.Length != 0 && txtTo.Text.Length == 0)
            IHeader = IHeader.Where(obj => obj.ReportedOn >= DateTime.Parse(txtFrom.Text)).ToList();
        else if (txtFrom.Text.Length == 0 && txtTo.Text.Length != 0)
            IHeader = IHeader.Where(obj => obj.ReportedOn <= DateTime.Parse(txtTo.Text)).ToList();
        else ;

}

于 2013-09-02T09:34:00.833 回答