DataGridView使用没有DataTable的TextBox(C#)进行过滤

时间:2016-03-27 20:14:36

标签: c# sqlite datagridview filter filtering

我正在为此项目使用SQlite数据库,我希望通过DataGridView过滤TextBox中的某些数据。

我在StackOverflow上阅读了其他帖子,但它们并没有解决我的问题。

将数据加载到DataGridView的函数:

private void loadDataToDataGrideView()
{
    using (SQLiteConnection connection = new SQLiteConnection(stringConnection))
    {
        try
        {
            connection.Open();
            stringSql = @"SELECT * FROM `tbl_user`;";
            SQLiteCommand cmd = new SQLiteCommand(stringSql,  connection);
            SQLiteDataReader readData = cmd.ExecuteReader();

            while (readData.Read())
            {
                dataG_Customer.Rows.Add(new object[] {
                readData.GetValue(readData.GetOrdinal("user_name")),
                readData.GetValue(readData.GetOrdinal("user_family")),
                readData.GetValue(readData.GetOrdinal("user_id_card")),
                readData.GetValue(readData.GetOrdinal("user_mobile")),
                readData.GetValue(readData.GetOrdinal("user_phone")),
                readData.GetValue(readData.GetOrdinal("user_regiter_date")),                                     readData.GetValue(readData.GetOrdinal("user_address")),
                readData.GetValue(readData.GetOrdinal("id")),
                readData.GetValue(readData.GetOrdinal("user_status"))
                });
            }
            connection.Close();
        }
            catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

在文本框中我使用此代码:

private void txt_search_TextChanged(object sender, EventArgs e)
{
    BindingSource bs = new BindingSource();
    bs.DataSource = dataG_Customer.DataSource;
    bs.Filter = "name" + " like '%" + txt_search.Text + "%'";
    dataG_Customer.DataSource = bs;
}

0 个答案:

没有答案