Reputation: 153
I want to filter data on the textchange event on listview so I use dataview to filter data. Issue in the below code is, I use dataview inside for each so that it checks only one condition that is last value only it takes, I want to check value in s1 with dataview and remaining value should bind with listview.
eg: if I type an in textbox it should list all the item values starting with an value like anandha kumar,anna ect. suppose I keep the value anandha kumar and anna in array s1. I should list all other values expect the array values like antony ect... in listview.
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
dvProducts = (DataView)Session["ListViewItems"];
string serachText = EscapeLikeValue(TextBox1.Text);
string lvValues = hdRetailCustomerGroup.Value;
string trim = lvValues.Replace(" ", "");
trim = trim.Replace("\r", "");
trim = trim.Replace("\n", "");
trim = trim.Replace("\t", "");
string str = trim;
string[] list = str.Split('|');
foreach (string s1 in list)
{
if (s1 != string.Empty)
{
dvProducts.RowFilter = "(CODE like '" + serachText + "*') AND (CODE <> '" + s1 + "')";
Session["ListViewItems"] = dvProducts;
}
}
ListView1.DataSource = dvProducts;
ListView1.DataBind();
}
Upvotes: 12
Views: 148875
Reputation: 341
I hope that is not too late,
If you want to filter your DataGrid
in an offline-locally-fashion here is the code:
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace SomeApp
{
public partial class MainWindow : Window
{
// they should be public to the Window
private DataSet ds;
private DataView dv;
public MainWindow()
{
InitializeComponent();
// loading data to the GridView
LoadData();
}
private async void LoadData()
{
// sql related and data fetch stuff
// get your data in a DataSet
dv = new DataView(ds.Tables[0]);
DataGrid1.ItemsSource = dv;
}
private void FilterTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
string filterText = FilterTextBox.Text;
if (string.IsNullOrEmpty(filterText))
{
dv.RowFilter = string.Empty;
}
else
{
var filterExpression = string.Join(" OR ", ds.Tables[0].Columns
.Cast<DataColumn>()
.Select(col => $"CONVERT([{col.ColumnName}], 'System.String') LIKE '%{filterText}%'"));
dv.RowFilter = filterExpression;
}
}
}
}
for the large data, however you need to let the server to do the job for you
Upvotes: 0
Reputation: 196
For example:
Datatable newTable = new DataTable();
foreach (string s1 in list)
{
if (s1 != string.Empty)
{
dvProducts.RowFilter = "(CODE like '" + searchText + "*') AND (CODE <> '" + s1 + "')";
foreach (DataRow dr in dvProducts.ToTable().Rows)
{
newTable.ImportRow(dr);
}
}
}
ListView1.DataSource = newTable;
ListView1.DataBind();
Upvotes: 7
Reputation: 391
DataView view = new DataView();
view.Table = DataSet1.Tables["Suppliers"];
view.RowFilter = "City = 'Berlin'";
view.RowStateFilter = DataViewRowState.ModifiedCurrent;
view.Sort = "CompanyName DESC";
// Simple-bind to a TextBox control
Text1.DataBindings.Add("Text", view, "CompanyName");
Ref: http://www.csharp-examples.net/dataview-rowfilter/
http://msdn.microsoft.com/en-us/library/system.data.dataview.rowfilter.aspx
Upvotes: 23