Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在任何文本框中的任何更改上更新过滤器和数据网格_C#_Wpf - Fatal编程技术网

C# 在任何文本框中的任何更改上更新过滤器和数据网格

C# 在任何文本框中的任何更改上更新过滤器和数据网格,c#,wpf,C#,Wpf,我有4个文本框。我使用它们中的每一个作为DataGrid的过滤器参数。问题是,我必须使用IF函数覆盖所有可能的变体,以使它们正常工作。有没有更好的办法 下面是一个示例,它甚至没有涵盖所有变体。我还需要分别为每个texbox修改它 代码如下: private void BusinessIDSearch_PreviewKeyDown(object sender, KeyEventArgs e) { _conditions["name"] = nul

我有4个文本框。我使用它们中的每一个作为DataGrid的过滤器参数。问题是,我必须使用IF函数覆盖所有可能的变体,以使它们正常工作。有没有更好的办法

下面是一个示例,它甚至没有涵盖所有变体。我还需要分别为每个texbox修改它

代码如下:

    private void BusinessIDSearch_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        _conditions["name"] = null;

        if (!string.IsNullOrEmpty(BusinessIDSearch.Text))
        {
            _conditions["name"] = string.Format("LY Like '{0}%'", BusinessIDSearch.Text);
            UpdateFilter();
            FICount.Content = DataGrid1.Items.Count;
        }
        if (!string.IsNullOrEmpty(NameSearch.Text) && string.IsNullOrEmpty(BusinessIDSearch.Text))
        {
            _conditions["name"] = string.Format("HAKUNIMI Like '%{0}%' AND LY Like '%{1}%'", NameSearch.Text, BusinessIDSearch.Text);
            UpdateFilter();
            FICount.Content = DataGrid1.Items.Count;
        }
        if (!string.IsNullOrEmpty(NameSearch.Text) && !string.IsNullOrEmpty(GroupSearch.Text))
        {
            _conditions["name"] = string.Format("HAKUNIMI Like '%{0}%' AND KONSERNI Like '%{1}%' AND YRNRO Like '{2}%'", NameSearch.Text, GroupSearch.Text, IDSearch.Text);
            UpdateFilter();
            FICount.Content = DataGrid1.Items.Count;
        }
        if (!string.IsNullOrEmpty(BusinessIDSearch.Text) && !string.IsNullOrEmpty(GroupSearch.Text) && !string.IsNullOrEmpty(NameSearch.Text) && !string.IsNullOrEmpty(IDSearch.Text))
        {
            _conditions["name"] = string.Format("HAKUNIMI Like '%{0}%' AND KONSERNI Like '%{1}%' AND YRNRO Like '{2}%' AND LY Like '{3}%'", NameSearch.Text, GroupSearch.Text, IDSearch.Text, BusinessIDSearch.Text);
            UpdateFilter();
            FICount.Content = DataGrid1.Items.Count;
        }
    }

更新:


此应用程序仅在某些本地计算机上使用,因此不应存在SQL注入风险。但是我知道这不是最好的“编码”。

像这样的东西怎么样:

其思想是依次检查每个参数并建立查询字符串。还要注意,您不应该盲目地将用户输入的文本传递给数据库——您需要防止SQL注入

    string qry = null;

    if (!string.IsNullOrEmpty(BusinessIDSearch.Text))
    {
        qry = string.Format("LY Like '{0}%'", DeInjectText(BusinessIDSearch.Text));
    }
    if (!string.IsNullOrEmpty(NameSearch.Text))
    {
        if (!string.IsNullOrEmpty(qry))
            qry += " AND ";
        qry += string.Format("HAKUNIMI Like '%{0}%'", DeInjectText(NameSearch.Text));
    }
    if (!string.IsNullOrEmpty(GroupSearch.Text))
    {
        if (!string.IsNullOrEmpty(qry))
            qry += " AND ";
        qry += string.Format("KONSERNI Like '%{0}%'", DeInjectText(GroupSearch.Text));
    }
    ....
    if (!string.IsNullOrEmpty(qry))
    {
        UpdateFilter();
        FICount.Content = DataGrid1.Items.Count;
    }

public string DeInjectText(text)
{
    // Do stuff here to remove SQL injection danger from text
    return text;
}

1) WPF和MVVM:使用绑定代替事件,在属性设置器中调用视图模型的方法
UpdateFilter()。2) 您可以动态创建过滤器:
X Like…
=string.Join+”和“separator”。对于工作代码考虑下次使用。将相关条件添加到列表中,然后<代码>条件= String。代码>。这样,您就不必手动编写所有可能的条件组合。@grek40您能提供一个例子来回答将来可能会发现这一点的其他人吗?@hatman,“……不应该有SQL注入风险”是一种危险的思维方式。您的代码超出了您的范围-有人将在其他地方使用它,并且/或者您的应用程序将向您公司以外的其他人公开。此外,您公司中的某个人可以决定“玩”。我总是为SQL注入和其他漏洞编写代码。@n8wrl这是真的!我将重写一切之前,它得到别人的手在一个适当的方式!谢谢你!我有一个想法,但是DeInjectText似乎给出了一个错误,尽管它看起来是正确的。我只是想知道我在做什么wrong@hatman需要有关DeInjectText错误的更多信息。请记住,我的函数示例没有任何作用-您需要决定什么对您有意义,也许这是您项目中的一个更全局的函数。你会想过滤掉各种字符组合,如开始注释、结束引号等。谷歌搜索更多信息