Actually your code needs several improvements. First you should use the property DataSource
of the component ComboBox
to fill in the items so that you can use the component’s resources correctly. To do this, create a class ViewModel
to the ComboBox
:
public class ComboViewModel
{
public string Text { get; set; }
public string Value { get; set; }
}
This class will be your template so create a temporary list that will receive your items and then seven this list as the property DataSource
of the component.
List<ComboViewModel> listaTemporaria = new List<ComboViewModel>()
{
new ComboViewModel { Text = "TODOS", Value = "000000" }
};
listaTemporaria.AddRange(recSituacao.Select(sit => new ComboViewModel{ Text = sit.DESCRICAO, Value = sit.ID_SITUACAO }).ToList());
cmbSituacao.DataSource = listaTemporaria;
cmbSituacao.DisplayMember = "Text";
cmbSituacao.ValueMember = "Value";
cmbSituacao.SelectedIndex = 0;
Before this when accessing the selected value in Combobox use the code below:
ComboViewModel itemSelecionado = cmbSituacao.SelectedItem as ComboViewModel;
if (itemSelecionado != null && itemSelecionado.Text != "TODOS")
filtro = filtro.And(s => s.ID_SITUACAO == itemSelecionado.Value);
Any questions let me know, I am without Visual Studio now and I did in the notebook, there may be some mistake.
You can’t, why?
– Leandro Angelo
because when I access the Selectedit it does not provide the "Text" nor the "Value", there in the image it is returning the Text because I am using the Getitemtext, but I really need to access the "Value" and this I can not for anything, any idea?
– user2929918
as the name says, it’s a
SelectedItem
, that is, the same object that you created, and from it you can access Text and Value– Ricardo Pontual
Trade the image for your code
– Leandro Angelo
Thank you, the concept I know, I want to understand how to access, you would know, using my code above? Ricardo Punctual
– user2929918