C
C#•19h ago
Azim

Is there a way to make a search bar for a datagrid in WPF?

Hi all, I'm using the entity framework for my database, I'm trying to make a search bar for one of the databases but I'm struggling on how to do it. Is there any advise or guides that can help me to make this, I had a look only but couldn't find anything related to my issue...unless I'm looking in the wrong place. I can send my code if needed.
2 Replies
JRamos
JRamos•17h ago
naive solution: Have datagrid reference some observableCollection<stuff> VisibleRows. Have a separate textbox and button. Have a button populate VisibleRows from a list of all of your data based on the TextBox content.
<Button content="filter" Command={Binding RepopulateCommand} />
<TextBox text={binding UserText} />
<Button content="filter" Command={Binding RepopulateCommand} />
<TextBox text={binding UserText} />
private List<Stuff> _stuffs = [];
public string UserText {get;set;} = null!;
public observableCollection<Stuff> Stuffs {get;set;} = [];
[RelayCommand] //from MVVM Community Toolkit
private void Repopulate(){
if(string.isemptyorwhtiespace(UserText)){return;}
var data = _stuffs.where(s => s.Name == UserText));
Stuffs.ReplaceWith(data);
}
private List<Stuff> _stuffs = [];
public string UserText {get;set;} = null!;
public observableCollection<Stuff> Stuffs {get;set;} = [];
[RelayCommand] //from MVVM Community Toolkit
private void Repopulate(){
if(string.isemptyorwhtiespace(UserText)){return;}
var data = _stuffs.where(s => s.Name == UserText));
Stuffs.ReplaceWith(data);
}
Azim
Azim•17h ago
thank you, I'll try that and see how it goes 🙂
Want results from more Discord servers?
Add your server