BindingSource Filter - 从字符串中删除单词

时间:2017-06-27 09:30:01

标签: c# checkbox filter datagrid bindingsource

我有一个附加到BindingSource的DGrid,它充满了数据。我试图使用复选框为用户过滤掉名为“状态”的特定列中的数据。我有四个复选框,因为同一列中将有四个不同的数据文本(列位于最右侧)。 Picture here

显然,当用户选中/取消选中其中一个复选框时,它会为网格设置一个过滤器,以在列中添加/删除该数据。

这是我的代码,适用于选中复选框时:

private void chkProgress_CheckedChanged(object sender, EventArgs e)
    {
        string filter = "";

        if (chkProgress.Checked)
        {

            filter = "Status LIKE '%Progress%'";

            if (chkPending.Checked)
            {
                filter += " Or Status LIKE '%Pending%'";
            }
            else
            {

            }

            if (chkCompleted.Checked)
            {
                filter += " Or Status LIKE '%Completed%'";
            }
            else
            {

            }

            if (chkArchived.Checked)
            {
                filter += " Or Status LIKE '%Archived%'";
            }
            else
            {

            }
        }
        else
        {



        }

        MessageBox.Show(filter);
        bs.Filter = string.Format(filter);
        refreshGrid();

    }

    private void chkCompleted_CheckedChanged(object sender, EventArgs e)
    {
        string filter = "";

        if (chkCompleted.Checked)
        {

            filter = "Status LIKE '%Completed%'";

            if (chkPending.Checked)
            {
                filter += " Or Status LIKE '%Pending%'";
            }
            else
            {

            }

            if (chkProgress.Checked)
            {
                filter += " Or Status LIKE '%Progress%'";
            }
            else
            {

            }

            if (chkArchived.Checked)
            {
                filter += " Or Status LIKE '%Archived%'";
            }
            else
            {

            }
        }
        else
        {



        }

        MessageBox.Show(filter);
        bs.Filter = string.Format(filter);
        refreshGrid();
    }

    private void chkArchived_CheckedChanged(object sender, EventArgs e)
    {
        if (chkArchived.Checked)
        {

        }
        else
        {

        }
    }

    private void chkPending_CheckedChanged(object sender, EventArgs e)
    {

        string filter = "";

        if (chkPending.Checked )
        {

            filter = "Status LIKE '%Pending%'";

            if( chkProgress.Checked )
            {
                filter += " Or Status LIKE '%Progress%'";
            }
            else
            {

            }

            if( chkCompleted.Checked )
            {
                filter += " Or Status LIKE '%Completed%'";
            }
            else
            {

            }

            if( chkArchived.Checked )
            {
                filter += " Or Status LIKE '%Archived%'";
            }
            else
            {

            }
        }
        else
        {


        }

        MessageBox.Show(filter);
        bs.Filter = string.Format(filter);
        refreshGrid();
    }

如果取消选中字符串过滤器中的数据,我将如何进行删除?与 - =?

类似的东西

1 个答案:

答案 0 :(得分:0)

这个怎么样:

if (chkPending.Checked)
{
  filter += " Or Status LIKE '%Pending%'";
}
else
{
  filter = filter.Replace(" Or Status LIKE '%Pending%'", "");
}