Last active
August 29, 2015 14:18
-
-
Save neo125874/492fe9a2a4cd2c82bd70 to your computer and use it in GitHub Desktop.
pass dt & conditions to get the filtered data(For Data Row)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// return filtered data | |
/// </summary> | |
/// <param name="receivedDt">original data</param> | |
/// <param name="contentCondition">filter condition</param> | |
/// <returns>new data table</returns> | |
public static DataTable GetDataFilter(DataTable receivedDt, List<TEUserObjectReportFilterModel> contentCondition) | |
{ | |
DataTable newDt = null; | |
try | |
{ | |
//conditions to filter data | |
StringBuilder sb = new StringBuilder(); | |
foreach (TEUserObjectReportFilterModel condition in contentCondition) | |
{ | |
sb.Append(','); | |
sb.Append("'" + condition.strFilterValue1.ToString().Trim() + "'"); | |
} | |
//query builder | |
StringBuilder qb = new StringBuilder(); | |
if (sb.Length > 0) | |
{ | |
sb.Remove(0, 1); | |
qb.AppendFormat(contentCondition.First().strFilterField.ToString() + " in ({0})", sb.ToString()); | |
} | |
//original columns | |
string[] columns = (from column in receivedDt.Columns.Cast<DataColumn>() select column.ColumnName).ToArray(); | |
receivedDt.DefaultView.RowFilter = qb.ToString();//default view: different dimensions to filter data | |
newDt = receivedDt.DefaultView.ToTable(true, columns); | |
//first: filter column(this is unchange) | |
//second: filter data row(this is what this function doing) | |
} | |
catch (Exception e) | |
{ | |
logger.Fatal("GetDataFilter:" + e.Message); | |
} | |
return newDt; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment