Click here to Skip to main content
16,018,973 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have window form & microsoft excel as database. I want to implement searching feature. i have 1 textbox 1 datagridview & 1 button & want whenever i click on button a search should be made in excel file based on the id provided in textbox & its description should be displayed in gridview.
The code i'm using is not dynamic its static i mean it'll only show description of data i provided in code & not in textbox ?
Help me i urgently need this code ????
My Code is

C#
private void srch()
        {
            string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= 'c:\\Product Details.xlsx';Extended Properties='Excel 8.0;HDR=Yes;'";
            // double id = Convert.ToDouble(textBox1.Text);
            string query = "SELECT * FROM [Sheet1$]";

            DataSet excelDataSet = new DataSet();
            OleDbDataAdapter da = new OleDbDataAdapter(query, strConn);
            da.Fill(excelDataSet);
            dataGridView1.DataSource = excelDataSet.Tables[0];
            DataView dv = ((DataTable)dataGridView1.DataSource).DefaultView;            
            DataView dv_filter = new DataView();
            dv_filter.Table = excelDataSet.Tables[0];            
            dv_filter.RowFilter = "ID = '105'";
            dataGridView1.DataSource = dv_filter;
                       
        }
Posted

Change the query.
C#
string query = "SELECT * FROM [Sheet1$] where ID= 105";

For more info read read this post
 
Share this answer
 
v2
C#
private void gridQueryAndReload()
        {
            if (ds.Tables.Count > 0)
            {
                string strSearch = this.txtSearch.Text.Trim();
                string[] strParam = new string[2];
                DataView dv = ds.Tables[0].DefaultView;
                string strQuery = "";

                strParam[0] = strSearch.Replace("'", "''").Replace("*", "").Replace("[", "]");
                strParam[1] = this.txtIDNumber.Text.Trim();

                try
                {
                    strQuery = string.Format(@"([{1}] like '{0}%')
                                        ", strParam);
                    dv.RowFilter = strQuery;
                }
                catch (Exception ex)
                {
                    this.txtSearch.Focus();
                }
                this.dataGridView1.DataSource = dv;
            }
 
Share this answer
 
okkkk. But in place of ID = 105, i want dynamic functionality like ID = textbox1.text where textbox1 takes id as input & want to show result based on based on that ID
 
Share this answer
 
Comments
Shanu2rick 13-Jul-13 3:53am    
then repalce 105 with textbox1.text
select * from [Sheet1$] where id='"+textbox1.text+"';
Datatable dt = new DataTale();

and after selecting the rows i use this code:

datagridview.DataSource=dt;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900