How to make Filtering for a DataTable?

Feb 22, 2016 at 11:47 AM
Edited Feb 22, 2016 at 11:48 AM
Hello to all, I made it my program read the excel and only let the filled rows in a DataTable like this code:
using System;
using System.Data;
using System.IO;
using Excel;
using System.Windows.Forms;

namespace SambaNet.ConversaoPlanilhaExcel.Classes
{
    public class LerPlanilha
    {
        IExcelDataReader excelReader = null;
        FileStream stream = null;
        public string tipo { get; set; }
        public string filePath { get; set; }
        bool erro = false;
        DataSet ds = null;
        DataTable dt = null;

        //RETORNA UMA DATATABLE COM A SHEET ESCOLHIDA, NO CASO A 1 - Produtos
        public DataTable Ler()
        {
            string extension = Path.GetExtension(filePath);
            try
            {
                stream = File.Open(filePath, FileMode.Open, FileAccess.Read);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                erro = true;
            }
            if (!erro)
            {
                switch (extension)
                {
                    case ".xls":
                        excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
                        break;
                    case ".xlsx":
                        excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
                        break;
                }

                excelReader.IsFirstRowAsColumnNames = true;
                ds = excelReader.AsDataSet();
                dt = ds.Tables[1];
                dt = RemoverRows(dt);
                FecharConexao();
                return dt;
            }
            else
            {
                FecharConexao();
                return null;
            }
        }

        //RETORNA A DATATABLE SEM AS ROWS VAZIAS.
        public DataTable RemoverRows(DataTable dt)
        {
            int columnCount = dt.Columns.Count;

            for (int i = dt.Rows.Count - 1; i >= 0; i--)
            {
                bool allNull = true;
                for (int j = 0; j < columnCount; j++)
                {
                    if (dt.Rows[i][j] != DBNull.Value)
                    {
                        allNull = false;
                    }
                }
                if (allNull)
                {
                    dt.Rows[i].Delete();
                }
            }
            dt.AcceptChanges();
            return dt;
        }

        public void FecharConexao()
        {
            excelReader.Close();
        }
    }
}
Now, I would like to make a filter system, for example:
  • If my id is duplicated, send a message.
  • If my description contains special characters, send a warning.
How could I develop it? Thank you, I enjoyed this reader!