2016-05-29 22 views
2

私は、必要なレコードを得るために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; 
    } 
} 
+0

同じ状況で私はjQueryを使用しました。私のページには、入力フィールドがあり、その下にSQL Serverからデータをロードするdivテーブルがありました。ユーザーが入力フィールドに何かを入力すると、以下の表はユーザーの入力に基づいてフィルタリングされます。 – gofr1

+0

明確にしてください:タイトルが示唆しているように、検索したいのですか?どちらもここでは非常に頻繁に答えられています。 – TaW

+0

@TaW - フィルタリングするために、私は自分自身を間違って説明しました。しかし、私は 'KeyUp'イベントを使ってそれを行うことができました。 – cdrrrrrr

答えて

0

私はprivate void txtNumber_KeyUp(object sender, KeyEventArgs e)イベントを使用し、それがうまく働きました。