none
Filter datatable rows SetColumnError RowErrors RRS feed

  • Question

  • I'm manually populating a datatable with data.  As I populate a row I validate certain aspects of the data and if a value fails, I set an error using SetColumnError.  Once I'm done I bind the datatable to a grid so a user can view the data, sort and filter it's contents.

    I want to add a feature that would allow the user to click a button and only see records in the grid that have a row error.  I have searched and cannot find anything.  My first shot was trying to find a way to produce a dataview by just filtering where rowerror exist.  No Luck.

    I assume I could loop through the table and as rows with errors are encountered write them to another table.  I know I can perform a search on a datatable using select and datarowstate, but row errors are not part of the rowstate.
    Tuesday, August 18, 2009 6:02 PM

Answers

  • You will have to iterate through the rows and pull out the ones you want.

    // dt being your datatable object that has errors
    // newdt is an empty datatable
    
    DataTable newdt = new DataTable();
    
    foreach (DataRow dr in dt.Rows)
    {
       if (dr.HasErrors)
       {
           // add your row to another datatable or put into array
           newdt.Rows.Add(dr);
       }
    
    }



    Tuesday, August 18, 2009 7:19 PM
  • If you are using .net framework 3.5 you could use Linq to DataSet to do an operation like this

    dataTable.AsEnumerable().Where(dr=>dr.HasErrors).AsDataView();



    Its really simple, other than that you could do what Michael has suggested above.

    Thanks
    Chris Robinson
    Program Manager - DataSet


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Tuesday, August 18, 2009 10:54 PM

All replies

  • Did you try the DataTable.Select() method to get your filtered results? It loads the result into an array to which you can bind.

    Tuesday, August 18, 2009 6:50 PM
  • I use the datatable.select very often.  But in this case I'm not attempting to get data back based on an expression of a field within the datatable.  I'm looking to get the rows within the datatable where datatable.row haserrors.
    Tuesday, August 18, 2009 7:08 PM
  • You will have to iterate through the rows and pull out the ones you want.

    // dt being your datatable object that has errors
    // newdt is an empty datatable
    
    DataTable newdt = new DataTable();
    
    foreach (DataRow dr in dt.Rows)
    {
       if (dr.HasErrors)
       {
           // add your row to another datatable or put into array
           newdt.Rows.Add(dr);
       }
    
    }



    Tuesday, August 18, 2009 7:19 PM
  • If you are using .net framework 3.5 you could use Linq to DataSet to do an operation like this

    dataTable.AsEnumerable().Where(dr=>dr.HasErrors).AsDataView();



    Its really simple, other than that you could do what Michael has suggested above.

    Thanks
    Chris Robinson
    Program Manager - DataSet


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Tuesday, August 18, 2009 10:54 PM
  • Just as a quick minor correction, the DataTable.Select returns an Array of DataRow, but it is not bindable. If you want bindable results you can use a DataView or use LINQ [which Chris demonstrated]
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Tuesday, August 18, 2009 11:39 PM
  • Old thread I know, but thought I'd mention this since I came across it my searchings.

    You don't have to iterate through all rows of the table and check them manually, just call the GetErrors Method on the table:

    if (dataTable.HasErrors)
    {
         foreach (DataRow dr in dataTable.GetErrors())
         {
              MessageBox.Show(dr.RowError.ToString());
         }
    }
    

    Hope it might help someone searching later.

    Thursday, October 6, 2011 2:29 PM