private Dictionary<string, PortStatus> _dicPortStatus = new Dictionary<string, PortStatus>();
private void cmbGroups_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//Combo box selection changed. Re-bind data
string selectedGroup = (string)cmbGroups.SelectedItem;
//Re-bind the grid
dgPortStatus.DataContext = _dicPortStatus[selectedGroup].Portstatus.DefaultView;
}
private void txtFilterIn_TextChanged(object sender, TextChangedEventArgs e)
{
FilterDataGrid(txtFilterIn.Text, txtFilterOut.Text, _dicPortStatus[cmbGroups.SelectedItem.ToString()]);
}
private void btnFilterInClear_Click(object sender, RoutedEventArgs e)
{
txtFilterIn.Clear();
}
private void txtFilterOut_TextChanged(object sender, TextChangedEventArgs e)
{
FilterDataGrid(txtFilterIn.Text, txtFilterOut.Text, _dicPortStatus[cmbGroups.SelectedItem.ToString()]);
//((CollectionView)dgPortStatus.ItemsSource).Refresh();
}
private void btnFilterOutClear_Click(object sender, RoutedEventArgs e)
{
txtFilterOut.Clear();
}
private void FilterDataGrid(string inText, string outText, DataSet ds)
{
if (ds != null )
{
if (!string.IsNullOrEmpty(inText) || !string.IsNullOrEmpty(outText))
{
foreach (DataTable dt in ds.Tables)
{
StringBuilder sbFilter = new StringBuilder();
foreach (DataColumn dc in dt.Columns)
{
if (dc.DataType == typeof(string))
{
if (!string.IsNullOrEmpty(inText))
{
if (sbFilter.Length > 0)
sbFilter.Append(" OR ");
sbFilter.Append("(");
sbFilter.Append(dc.ColumnName + " LIKE '%" + inText + "%'");
}
if (!string.IsNullOrEmpty(outText))
{
if (sbFilter.Length > 0)
sbFilter.Append(" AND ");
if (string.IsNullOrEmpty(inText))
sbFilter.Append("(");
sbFilter.Append(dc.ColumnName + " NOT LIKE '%" + outText + "%'");
}
sbFilter.Append(")");
}
if (dc.DataType == typeof(Int32) || dc.DataType == typeof(Double))
{
if (!string.IsNullOrEmpty(inText))
{
if (sbFilter.Length > 0)
sbFilter.Append(" OR ");
sbFilter.Append("(");
sbFilter.Append("CONVERT(" + dc.ColumnName + ", System.String)" + " LIKE '%" + inText + "%'");
}
if (!string.IsNullOrEmpty(outText))
{
if (sbFilter.Length > 0)
sbFilter.Append(" AND ");
if (string.IsNullOrEmpty(inText))
sbFilter.Append("(");
sbFilter.Append("CONVERT(" + dc.ColumnName + ", System.String)" + " NOT LIKE '%" + outText + "%'");
}
sbFilter.Append(")");
}
}
dt.DefaultView.RowFilter = sbFilter.ToString();
}
}
else
{
foreach (DataTable dt in ds.Tables)
{
dt.DefaultView.RowFilter = String.Empty;
}
}
}
}