通过在文本框中键入来过滤

时间:2016-05-29 09:13:08

标签: c# sql-server winforms datagridview

我正在使用下面的代码来绑定我的datagridviewselect语句,以便获取我需要的记录。

在这些代码行中一切正常,但我想在用户输入textbox内容时自动过滤记录,当删除数据时,记录返回初始状态,当表单是加载。谁能给我一些提示?

这是我的代码:

public nir()
{
    InitializeComponent();
    nir_load();
}

void nir_load()
{
    string cs = "Data Source=IS020114\\CODRINMA;Initial Catalog=gcOnesti;Integrated Security=True";
    string select = "select p.cod as Numar, p.data as Data, p.doc_cod as NrDocFurnizor, g.nume as Gestiune, c.nume as Furnizor, p.validat as Validat, p.facturat as Contat from primar p inner join gestiuni g on p.part2=g.gest_id inner join cf c on p.part1=c.cf_id where p.tip=2";

    using (SqlConnection con = new SqlConnection(cs))
    {
        SqlCommand cmd = new SqlCommand(select, con);

        con.Open();
        SqlDataAdapter sda = new SqlDataAdapter();
        sda.SelectCommand = cmd;

        DataTable dt = new DataTable();
        sda.Fill(dt);

        BindingSource bs = new BindingSource();
        bs.DataSource = dt;
        dataGridView1.DataSource = bs;
    }
}

private void btnSearch_Click(object sender, EventArgs e)
{
    string cs = "Data Source=IS020114\\CODRINMA;Initial Catalog=gcOnesti;Integrated Security=True";
    string select = "select p.cod as Numar, p.data as Data, p.doc_cod as NrDocFurnizor, g.nume as Gestiune, c.nume as Furnizor, p.validat as Validat, p.facturat as Contat from primar p inner join gestiuni g on p.part2=g.gest_id inner join cf c on p.part1=c.cf_id where cod='" +txtnr.Text+"' and data='" + dtpData.Value.ToString("yyyy-MM-dd") +"' and p.tip=2";

    using (SqlConnection con = new SqlConnection(cs))
    {
        SqlCommand cmd = new SqlCommand(select, con);

        con.Open();
        SqlDataAdapter sda = new SqlDataAdapter();
        sda.SelectCommand = cmd;

        DataTable dt = new DataTable();
        sda.Fill(dt);

        BindingSource bs = new BindingSource();
        bs.DataSource = dt;
        dataGridView1.DataSource = bs;
    }
}

1 个答案:

答案 0 :(得分:0)

我使用了private void txtNumber_KeyUp(object sender, KeyEventArgs e)事件并且工作正常。