加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 20:54:34 所属栏目:百科 来源:网络整理
导读:我在使用Textbox过滤datagrid值(来自数据库)时遇到了一些麻烦.事实上,我是 WPF C#的新手,在这种情况下我需要一些帮助. 这是我的XAML: Button Name="btnSelect" Content="Select All" Height="30" Width="80" Margin="4" HorizontalAlignment="Center" Click
我在使用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);
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读