0

Curently i am opening a .xls file, by entering the file location in a text box and i want to search a certain value by entering it in a text box and delete the row in which the value resides in.

Here is the code for Form1, that contains the dataGridView:

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();
        }
    }
}

This is Form2 that contains the textBox and button in which i want to enter the search value.

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();

        }
    }
}

Thank you for your help.

Stefan Siman
  • 49
  • 2
  • 10
  • you can can do this using the DataTable that was initially bound to the existing datagridview. the DataTable has a Filter propertyI will post a simple example on how easy this is.. – MethodMan Mar 25 '16 at 13:58
  • I think one of the options here will do it for you! http://stackoverflow.com/questions/31809201/how-to-use-textbox-to-search-data-in-data-grid-view – ASH Mar 25 '16 at 16:06

2 Answers2

0

I have a DataTable declared as at the class level.

protected static DataTable dtSearch = null; 

Then I have a button click that searches the DataTable to filter down based on the value that I have typed in a Textbox.Text field.

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;
    }
}
MethodMan
  • 18,625
  • 6
  • 34
  • 52
0

This worked for me :

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;
Stefan Siman
  • 49
  • 2
  • 10