微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用TextBox过滤Datagrid值(WPF C#)

我在使用TextBox过滤datagrid值(来自数据库)时遇到了一些麻烦.事实上,我是 WPF C#的新手,在这种情况下我需要一些帮助.

enter image description here

这是我的XAML:

<Button Name="btnSelect"
        Content="Select All"
        Height="30"
        Width="80"
        Margin="4"
        HorizontalAlignment="Center"
        Click="btn_SelectUser"/>
    <DataGrid Name="dtgUser" 
              AutoGenerateColumns="True" 
              HorizontalAlignment="Left" 
              VerticalAlignment="Top" Height="380" Width="684" 
              Margin="10,54,0"/>

这是我的Code Behind在按下Select按钮后从数据库中选择值:

private void btn_SelectUser(object sender,RoutedEventArgs e)
        {

            _con = new sqlConnection(_strConn);
            try
            {
                _con.open();
                string query = "select id_int_user,name_str_user  from tbl_user";
                _cmd = new sqlCommand(query,_con);
                _cmd.ExecuteNonQuery();

                _adp = new sqlDataAdapter(_cmd);
                _dt = new DataTable("tbl_user");
                _adp.Fill(_dt);
                dtgUser.ItemsSource = _dt.defaultview;
                _adp.Update(_dt);

                _con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

这是在文本框中选择刚刚编辑过的值.如何实现此方法? (注意:我需要通过datagrid从数据库获取值):

private void txt_SearchUser(object sender,TextChangedEventArgs e)
    {
        //Here is my difficulty //I kNow nothing how to do it.
    }

解决方法

您的方法可能不是填充DataGrid的最佳方法(您可能希望将来使用 data binding),但这里有一些针对您的情况的快速技巧:

1)如果用户需要先单击“全选”按钮,则按ID过滤数据:

private void txt_SearchUser(object sender,TextChangedEventArgs e)
{
    DataTable tempDt = _dt.copy();
    tempDt.Clear();
    if (txt_Search.Text != "") // Note: txt_Search is the TextBox..
    {
        foreach (DaTarow dr in _dt.Rows)
        {
            if (dr["id_int_user"].ToString() = txt_Search.Text)
            {
                tempDt.ImportRow(dr);
            }
        }
        dtgUser.ItemsSource = tempDt.defaultview;
    }
    else
    {
        dtgUser.ItemsSource = _dt.defaultview;
    }
}

2)如果您想在每次用户将ID放入搜索框时过滤数据库(不先单击全选按钮):

private void txt_SearchUser(object sender,TextChangedEventArgs e)
{
    _con = new sqlConnection(_strConn);
    try
    {
        _con.open();
        string query = "select id_int_user,name_str_user from tbl_user";
        if(txt_Search.Text != "") // Note: txt_Search is the TextBox..
        {
            query += " where id_int_user = " + txt_Search.Text;
        }
        _cmd = new sqlCommand(query,_con);
        _cmd.ExecuteNonQuery();

        _adp = new sqlDataAdapter(_cmd);
        _dt = new DataTable("tbl_user");
        _adp.Fill(_dt);
        dtgUser.ItemsSource = _dt.defaultview;
        _adp.Update(_dt);

        _con.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐