Ciao.
Sono recentemente passato a C#.
Come si fa ad utilizzare una lista di tipo List<t> come datasource per un datagrid? In Vb.net una volta assegnato l'oggetto lista alla proprietà datasource andava. Ora nelle prove che sto facendo le righe non vengono aggiornate nella tabella.
Ho provato ad implementare gli oggetti come INotifyPropertyChanged e le liste come ObservableCollection ma nulla è cambiato.
Codice che ho usato
Elemento della lista:
codice:
public class FilterItem : INotifyPropertyChanged
{
private string _Pattern;
private string _Mask;
public String Pattern
{
get
{
return _Pattern;
}
set
{
_Pattern = value;
OnPropertyChanged("Pattern");
}
}
public String Mask
{
get
{
return _Mask;
}
set
{
_Mask = value;
OnPropertyChanged("Mask");
}
}
public FilterItem(String Pattern, String Mask)
{
this.Pattern = Pattern;
this.Mask = Mask;
}
public override string ToString()
{
return Mask + "\t" + Pattern;
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string info)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(info));
}
}
}
lista:
codice:
public class Filterlist : ObservableCollection<FilterItem>
{
public Filterlist()
{
base.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Filterlist_CollectionChanged);
}
void Filterlist_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
Debug.WriteLine(e.ToString());
}
public override string ToString()
{
String ret = String.Empty;
foreach (FilterItem item in this)
{
ret += item.ToString() + "\n";
}
return ret;
}
}
e usato:
codice:
[...]
public Filterlist Filters = new Filterlist();
[...]
dbwFilters.DataSource = Filters;
Idee?