We Recommend

Accelerated C# 2008 Accelerated C# 2008
This book is both a rapid tutorial and a permanent reference. You’ll quickly master C# syntax while learning how the CLR simplifies many programming tasks. You’ll also learn best practices that ensure your code will be efficient, reusable, and robust. Why spend months or years discovering the best ways to design and code C# when this book will show you how to do things the right way, right from the start?


Posted By

rengber on 01/31/08


Tagged

data filter ADONet


Versions (?)


Filter Expression for a DataTable


Published in: C# 


Pretty intuitive, might be an easier/simpler way.


  1. //This was the hard way.
  2. public static Lookup.StatusDataTable GetStatuses(bool intranetMode)
  3. {
  4. //TODO: Handle Intranet vs Internet mode.
  5. LookupTableAdapters.StatusTableAdapter sta = new LookupTableAdapters.StatusTableAdapter();
  6. Lookup.StatusDataTable sdt = sta.GetStatusData();
  7. DataRow[] rows = sdt.Select("Status <> 'Unapproved' and Status <> 'Closed'");
  8. Lookup.StatusDataTable retVal = new Lookup.StatusDataTable();
  9. foreach(DataRow dr in rows)
  10. {
  11. retVal.Rows.Add(dr.ItemArray);
  12. }
  13. return retVal;
  14. }
  15.  
  16. //Here's the easy way:
  17. public static Lookup.StatusDataTable GetStatuses(bool intranetMode)
  18. {
  19. LookupTableAdapters.StatusTableAdapter sta = new LookupTableAdapters.StatusTableAdapter();
  20. Lookup.StatusDataTable sdt = sta.GetStatusData();
  21. if (!intranetMode)
  22. {
  23. sdt.DefaultView.RowFilter = "Status <> 'Unapproved' and Status <> 'Closed'";
  24. }
  25. return sdt;
  26. }

Report this snippet 

You need to login to post a comment.