i have listview and textbox for search in listview every time user type in textbox i run new query, is there any better way to do that?without running query from database just from itemsources?
private void txtEditSearch_TextChanged(object sender, TextChangedEventArgs e)
{
if (txtEditSearch.Text != string.Empty)
{
var query = GetAllSchoolsAsync(txtEditSearch.Text);
query.Wait();
List<DataClass.Tables.School> data = query.Result;
if (data.Any())
dgv.ItemsSource = data;
}
else
getSchool();
}
i need something like this:
var basedata = dgv.Itemsource;
dgv.ItemSource = basedata.where(x=>x.Name == txtEditSearch.Text).Select(x=>x);
if the listview is populated with data you can filter on that data which acts just like a search, just displays the data you asked for. Here is the link I learnt about this from:
http://www.wpf-tutorial.com/listview-control/listview-filtering/
The best way, I recommend you, to achieve this is creating an initial unfiltered collection and filter from this collection.
Make a private field to store the initial School items:
private List<DataClass.Tables.School> _initialCollection;
Fill it with the unfiltered items in the contructor:
public MyView()
{
var query = GetAllSchoolsAsync();
query.Wait();
_initialCollection = query.Result;
}
In the TextChanged eventhandler you can add the filtering to theItemSource of the ListView:
private void txtEditSearch_TextChanged(object sender, TextChangedEventArgs e)
{
dgv.ItemSource = _initialCollection.Where(x=>x.Name == txtEditSearch.Text).Select(x=>x);
}
Related
My code is:
private void cmbMaritalStatus_Click(object sender, EventArgs e)
{
if (cmbMaritalStatus.BindingContext==null)
{
cmbMaritalStatus.Text = string.Empty;
FillComboboxes();
}
}
public void FillComboboxes()
{
cmbMaritalStatus.SetBindingToLookup(dataSource);
cmbMaritalStatus.BindSelected(bscAssistanceFileModel, pnr.Get(x => x.AssistanceFile.MaritalStatus));
}
the object dataSource is type of IEnumerable<CDX_MaritalStatus>
and I want that if this Combobox not was Binding so I send it to function
that bind it.
I try this code but in the first time it's looks good
But when that I debug it and I see that it's full, it's enter to the if
although that it's was Binding
The solution is only flag??
I donĀ“t know how you bind the data to the combobox. But you can try to check if the DataSource is null - like this:
DataSource source = cmbMaritalStatus.DataSource;
if (source.Count == 0)
{
cmbMaritalStatus.Text = string.Empty;
FillComboboxes();
}
I need to add a search box to a listbox that has data pulling from SQL - I'm not sure how as it isn't my code. I just need to add the search function. The listbox holds user names and surnames. So all I have to work with is lbUsers (the listbox name).
Thus far I have it to search a user name but it's only displaying the closest search - I want the code to filter out everything containing what I have typed into the search box:
private void btnSearch_Click(object sender, EventArgs e)
{
this.lbUsers.SelectedItems.Clear();
for (int s = this.lbUsers.Items.Count - 1; s >= 0; s--)
{
if (this.lbUsers.Items[s].ToString().ToLower().Contains(this.tbSearch.Text.ToLower()))
{
this.lbUsers.SetSelected(s, true);
}
}
}
I also don't want all the users to display - only those relating to the search box's criteria.
You will have to do this manually:
Save all users in a list
Filter the list accoring the text in the TextBox
Add the results to the ListBox
This is a minimal example:
List<User> users = new List<User>();
private void txtFilter_TextChanged(object sender, EventArgs e)
{
List<User> displayList = this.users;
if(this.txtFilter.Text != string.Empty)
{
displayList = this.users.Select(u => u.Name == this.txtFilter.Text);
}
this.lbUsers.Items.Clear();
this.lbUsers.Items.AddRange(displayList);
}
I think the best way to do this is through visibility. This way you don't have to keep creating/disposing of listbox items.
For example, the code below would do what you want:
foreach (var item in lbUsers.Items)
{
if (item.ToString().Contains(this.tbSearch.Text))
{
item.Visible = true;
}
else
{
item.Visible = false;
}
}
public void DD_Location()
{
var ctx = new LCCDB_EF();
var query = ctx.tbl_Location;
CB_Location.DataContext = query.ToList();
}
private void CB_Location_DropDownClosed(object sender, EventArgs e)
{
textbox_test.Text =CB_Location.Text;
}
Output in Textbox
System.Data.Entity.DynamicProxies.Location_5E43C6C196972BF0754973E48C9C941092D86818CD94005E9A759B70BF6E48E6
Try this
if(null != CB_Location.SelectedItem)
textbox_test.Text = CB_Location.SelectedItem.ToString();
Without seeing your XAML I can't be sure, but are you sure you've bound the list correctly? Try setting the Items property of combobox to your list, rather than the data context. Depending on what the type is and what you'd like to bind the text to, you may need to set the DisplayMemberPath property as appropriate, too.
One article has Name and Price properties. I use Name property to display articles inside combobox cmbDataList like this
public Form1()
{
InitializeComponent();
cmbDataList.DataSource = GetData();
cmbDataList.DisplayMember = "Name";
}
After user selected the preffered article I want to use it's Price property to assign to textbox on the same form. So, how to access to that Price property?
private void cmbDataList_SelectedIndexChanged(object sender, EventArgs e)
{
//var sel = cmbDataList.SelectedItem;
}
You have to cast SelectedItem to proper object.
private void cmbDataList_SelectedIndexChanged(object sender, EventArgs e)
{
var sel = (YourObject)cmbDataList.SelectedItem;
txt.Text = sel.Price.ToString();
}
Unless all names are unique, you're going to need a unique identifier to reference, for example an articleID.
From here, set the ComboBox's ValueMember like so;
cmbDataList.ValueMember = "ID";
then you can get your value on the event handler;
private void cmbDataList_SelectedIndexChanged(object sender, EventArgs e)
{
var sel = cmbDataList.SelectedValue;
//From here you're going to need to find your article with that particular ID.
}
Alternatively. You could have your DisplayMember as the article name, and the price as the ValueMember, then get it in the event handler for SelectedIndexChanged in the same way i put above. SelectedValue will then return the price;
cmbDataList.ValueMember = "Price";
private void cmbDataList_SelectedIndexChanged(object sender, EventArgs e)
{
var yourSelectedPrice = cmbDataList.SelectedValue;
}
Assuming GetData() returns a table, you need to write the ValueMember also... like this:
InitializeComponent();
cmbDataList.DataSource = GetData();
cmbDataList.DisplayMember = "Name";
cmbDataList.ValueMember = "Price";
Now, your selected display will be synced with the value and you will be able to use it..
Get more info in here:
Populate combobox
You Need to set ValueMember You can set in this way
cmbDataList.ValueMember = "ID";
then you write the code on cmbDataList_SelectedIndexChanged Event
May be this will help you
var sel = cmbDataList.SelectedValue
Currently my repository returns an IQueryable object that lists the data from my DB and I bind this to a BindingSource for use in a grid:
public void BindTo(IQueryable elements)
{
BindingSource source = new BindingSource();
source.CurrentChanged += new EventHandler(source_CurrentChanged);
source.DataSource = elements;
elementNavigator.BindingSource = source;
elementGridView.DataSource = source;
}
This works great. However I am wanting to do some stuff when a user clicks an row in the grid. I'm struggling to identify the element that the user is selecting. I have the following:
In my view:
private void source_CurrentChanged(object sender, EventArgs e)
{
_presenter.ElementChanged(sender, e);
}
In my presenter:
public void ElementChanged(object sender, EventArgs e)
{
BindingSource source = (BindingSource)sender;
// Here I need to get the ID of the selected element in the source.Current property.
// HOW?
}
This seems to work ok - and I can see when debugging that source.Current contains the data:
? source.Current
{ BodyId = 1, IsInUse = true, IsValid = true, CreateDate = {04/07/2006 09:31:59}, LastUpdateDate = {04/07/2006 09:31:59}, StatusDescShort = "Exist" ... }
BodyId: 1
CreateDate: {04/07/2006 09:31:59}
IsInUse: true
IsValid: true
LastUpdateDate: {04/07/2006 09:31:59}
StatusDescShort: "Exist"
but I am at a loss as how I might access the value of BodyId. I've a feeling I'm missing something really obvious here (not the first time).
I'm surprised you're not using something like IQueryable<MyType>.
Because then it would be a simple matter of casting: source.Current as MYType
And maybe eliminate an in-between DataRowView