TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Bradley George
NA
13
39.6k
Help with using two filters in one data grid!!
Dec 8 2010 10:45 AM
I have one datagrid that I am using to alternate between showing two different data sources. I have a filter working for one of the data sources, but it dosent work for the other one. Basically, one data grid showing the first data source, then i can click a button to switch to the other data source in the same grid but i cannot filter this one. Here is the code i have for the filter on the first data source that works:
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;
}
}
}
}
Reply
Answers (
1
)
passing multiple values from one page to another page
How to Update an item in a list by LINQ