Row filter for datatable

Mostafa alselk 1 Reputation point
2021-09-02T13:03:52.213+00:00

i have column its name : Mark and i want to get rows that start with "CB" and remaining text should be numbers
Ex: Rows
CBY1 10 50 ----> false because remaining text Y1 that not all number
CB1 20 100 ----> ok
CB2 30 200 -----> ok
what is row filter for dataview to get the rows thanks.

Developer technologies | C#
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. P a u l 10,761 Reputation points
    2021-09-02T19:58:24.467+00:00

    DataView.RowFilter doesn't support regular expression syntax, but it is possible to do the filtering with Linq and then convert the results back to a DataView if you need to use that type:

    DataTable dt = new DataTable("TestTable");
    
    dt.Columns.Add(new DataColumn("Mark", typeof(string)));
    dt.Columns.Add(new DataColumn("Value1", typeof(int)));
    dt.Columns.Add(new DataColumn("Value2", typeof(int)));
    
    dt.Rows.Add("CBY1", 10, 50);
    dt.Rows.Add("CB1", 20, 200);
    dt.Rows.Add("CB2", 30, 300);
    
    DataView dv = dt.AsEnumerable()
     .Where(row => Regex.IsMatch((string)row["Mark"], @"^CB\d+$"))
     .AsDataView();
    
    foreach (DataRowView item in dv) {
     Console.WriteLine(item["Mark"] + ", " + item["Value1"] + ", " + item["Value2"]);
    }
    
    1 person found this answer helpful.
    0 comments No comments

  2. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-09-02T23:22:36.177+00:00

    Taking @P a u l a step farther consider using a BindingSource which means there is zero reason to work with data via a DataGridView cell values. The example below uses @P a u l logic only layered on top of a BindingSource. Also added a primary key as all data should have one.

    using System;  
    using System.Data;  
    using System.Linq;  
    using System.Text.RegularExpressions;  
    using System.Windows.Forms;  
      
    namespace DataTableFilterExample  
    {  
        public partial class Form1 : Form  
        {  
            private readonly BindingSource _bindingSource =   
                new BindingSource();  
              
            public Form1()  
            {  
                InitializeComponent();  
                Shown += OnShown;  
            }  
      
            private void OnShown(object sender, EventArgs e)  
            {  
                _bindingSource.DataSource = LoadDataTable();  
                dataGridView1.DataSource = _bindingSource;  
            }  
      
            private void FilterButton_Click(object sender, EventArgs e)  
            {  
                DataTable table =   
                    (  
                        from dataRow in ((DataTable)_bindingSource.DataSource).AsEnumerable()   
                        where Regex.IsMatch(dataRow.Field<string>("Mark"), @"^CB\d+$")   
                        select dataRow  
                    )  
                    .ToArray()  
                    .CopyToDataTable();  
                  
                table.Columns["id"].ColumnMapping = MappingType.Hidden;  
                  
                _bindingSource.DataSource = table;  
                  
            }  
      
            private static DataTable LoadDataTable()  
            {  
                var dt = new DataTable();  
      
                dt.Columns.Add(new DataColumn()  
                {  
                    ColumnName = "Id",   
                    DataType = typeof(int),   
                    AutoIncrement = true,   
                    AutoIncrementSeed = 1,  
                    ColumnMapping = MappingType.Hidden  
                });  
                  
                dt.Columns.Add(new DataColumn("Mark", typeof(string)));  
                dt.Columns.Add(new DataColumn("Value1", typeof(int)));  
                dt.Columns.Add(new DataColumn("Value2", typeof(int)));  
      
                dt.Rows.Add(null,"CBY1", 10, 50);  
                dt.Rows.Add(null,"CB1", 20, 200);  
                dt.Rows.Add(null,"CB2", 30, 300);  
                  
                return dt;  
            }  
      
            private void CurrentButton_Click(object sender, EventArgs e)  
            {  
                if (_bindingSource.Current != null)  
                {  
                    var row = ((DataRowView) _bindingSource.Current).Row;  
      
                    MessageBox.Show($"Id:{row.Field<int>("id"),6:D3}\nValue1:{row.Field<int>("Value1"),6:D3}");  
                }  
                else  
                {  
                    MessageBox.Show("No current row");  
                }  
            }  
        }  
    }  
    
    
    
    
    
      
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.