简体   繁体   中英

Search in DataGridView (Excel File) + c#

I have a problem :-) I try to make a quick search with highlighting in a datagridview.

In my datagridview is an excel sheet.

        try
        {
            System.Data.OleDb.OleDbConnection MyConnection;
            System.Data.DataSet DtSet;
            System.Data.OleDb.OleDbDataAdapter MyCommand;
            MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\\test2.xls';Extended Properties=Excel 8.0;");
            MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection);
            MyCommand.TableMappings.Add("Table", "TestTable");
            DtSet = new System.Data.DataSet();
            MyCommand.Fill(DtSet);
            dataGridView1.DataSource = DtSet.Tables[0];
            MyConnection.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }

And I need it like this:

在此处输入图片说明

I have no idea, I hope someone can help me :-)

Thank you very mutch

1 - Load from excel to datatable ( which you already did it) then save DataTable to global Variable

2- When search clicked use DataTable.Select : example or since you are using oledb you can again select your excel with a simple query instead of Datatable select, up to you, you might give it a try if datatable select's performance is poor. Example excel searching

3- then Highlight your text in datagrid, i havent used windows forms such a long time , but maybe http://social.msdn.microsoft.com/forums/en-US/vbgeneral/thread/43f6b81f-4cb7-4e8e-bd29-e3645f200734/ might be a clue for you

Ok I used links i provided so far so good, except highliht everything work:

A Simple Helper class for reading excel and searching in DataTable:

public class ExcelHelper
    {
        public static DataTable LoadXLS(string strFile, String sheetName)
        {
            DataTable dtXLS = new DataTable(sheetName);
            try
            {
                string strConnectionString = "";
                if (strFile.Trim().EndsWith(".xlsx"))
                {
                    strConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";", strFile);
                }
                else if (strFile.Trim().EndsWith(".xls"))
                {
                    strConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";", strFile);
                }
                OleDbConnection SQLConn = new OleDbConnection(strConnectionString);
                SQLConn.Open();
                OleDbDataAdapter SQLAdapter = new OleDbDataAdapter();
                string sql = "SELECT * FROM [" + sheetName + "$] "; // "WHERE " + column + " = " + value;
                OleDbCommand selectCMD = new OleDbCommand(sql, SQLConn);
                SQLAdapter.SelectCommand = selectCMD;

                SQLAdapter.Fill(dtXLS);
                SQLConn.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            return dtXLS;
        }

        public  static DataTable QueryDataTable(DataTable dt, string column,string value)
        {
            var drs =  dt.Select(String.Format("{0} LIKE '%{1}%'", column, value));
            DataTable dt2 = dt.Clone();
            foreach (var dataRow in drs)
            {
                dt2.ImportRow(dataRow);
            }
            return dt2;
        }
    }

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM