-1

I have Window form and Microsoft Excel as database. I want to implement searching feature. I have 1 textbox 1 datagridview and 1 button and I want whenever I click on button a search should be made in Excel file based on the id provided in textbox and its description should be displayed in gridview.

The code I'm using is not dynamic, it's static. Does that mean it'll only show description of data I provided in code and not in textbox?

My code is

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;

    }
4

1 回答 1

0

do not select all the sheet use this code instead:

query="select * from [Sheet1$] where id='"+textbox1.text+"';
Datatable dt = new DataTale();

and after selecting the rows use this code:

datagridview.DataSource=dt;

hope it help

于 2013-07-13T07:45:49.703 回答