2016-03-25 10 views
0

私はxlsファイルを開いていますが、テキストボックスにファイルの場所を入力して、テキストボックスに入力して特定の値を検索したいその値が常駐する行を削除textBoxの値を検索してgridViewにあるデータで見つけます

ここでのDataGridViewが含まれているForm1のコードは、次のとおりです。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Data.OleDb; 

namespace Plan_de_lucru_1._0 
{ 
    public partial class frPlanMain : Form 
    { 
     public frPlanMain() 
     { 
      InitializeComponent(); 
     } 

     private void frPlanMain_Load(object sender, EventArgs e) 
     { 

     } 

     private void GoButton_Click(object sender, EventArgs e) 
     { 
      string constr = "Provider = MicroSoft.Jet.OLEDB.4.0; Data Source=" + locTBox.Text + "; Extended Properties =\"Excel 8.0; HDR=Yes;\";"; 
      OleDbConnection con = new OleDbConnection(constr); 
      OleDbDataAdapter sda = new OleDbDataAdapter("Select * From [" + shTBox.Text + "$]", con); 
      DataTable dt = new DataTable(); 
      sda.Fill(dt); 
      dGVPlan.DataSource = dt; 
      new SearchWindow().Show(); 
      this.Show(); 
     } 
    } 
} 

これは私が入力したいテキストボックスとボタンが含まれているのForm2であります検索値。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace Plan_de_lucru_1._0 
{ 
    public partial class SearchWindow : Form 
    { 
     public SearchWindow() 
     { 
      InitializeComponent(); 
     } 

     private void SearchButtonW_Click(object sender, EventArgs e) 
     { 
      String str = "select * from searchBox where (Name like '%' + @search + '%')"; 
      BindingSource bs = new BindingSource(); 

     } 
    } 
} 

ありがとうございました。

+0

を最初に既存のDataGridViewにバインドされたDataTableのを使用して、これを行うことができます。 DataTableにはFilterプロパティがあります。これがどれほど簡単かの簡単な例を投稿します。 – MethodMan

+0

私はここのオプションの1つがあなたのためにできると思います! http://stackoverflow.com/questions/31809201/how-to-use-textbox-to-search-data-in-data-grid-view – ryguy7272

答えて

0

私はクラスレベルで宣言されたDataTableを持っています。

protected static DataTable dtSearch = null; 

次に、TextBox.Textフィールドに入力した値に基づいてフィルタリングするためにDataTableを検索するボタンクリックがあります。

protected void btnSrch_Click(object sender, EventArgs e) 
{ 
    if (txtSearch.Text.Length > 0) 
    { 
     dtSearch = dtInitialDatable;//the datatable that was bound to my DatagridView 
     dv = new DataView(dtSearch); 
     dv.RowFilter = "Data_Column_Name LIKE '%" + txtSearch.Text.ToUpper() + "%'"; 
     //bind the DataGridView to dv 
     myDataGridView.DataSource = dv; 
     //myDataGridView.DataBind();//uncomment if you are doing this in webform 
    } 
    else 
    { 
     dtSearch = null; 
     dv = null; 
    } 
} 
0

これが私の仕事:

namespace Plan_de_lucru_1._0 
{ 
    public partial class SearchWindow : Form 
    { 
     public frPlanMain refTofrPlanMain; 

     public SearchWindow(frPlanMain f) //<<Edit made here 
     { 
      refTofrPlanMain = f; 
      InitializeComponent(); 
     } 

     private void SearchButtonW_Click(object sender, EventArgs e) 
     { 
      { 
       (refTofrPlanMain.dGVPlan.DataSource as DataTable).DefaultView.RowFilter = string.Format("Vodic = '{0}'", searchTBoxW.Text); 
       foreach (DataGridViewRow item in refTofrPlanMain.dGVPlan.Rows) 
       { 
        if (item.Visible) 
        { 
         item.Selected = true; 
         break; 
関連する問題