Когда я динамически добавляю первый объект в список, он выдает "System.InvalidOperationException" " без дополнительных объяснений почему.
Код: Выделить всё
internal class Frame : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string pgn;
private string b0;
private string b1;
public string PGN
{
get => pgn;
set { pgn = value; OnPropertyChanged(); }
}
[DisplayName("Byte 0")]
public string B0
{
get => b0;
set { b0 = value; OnPropertyChanged(); }
}
[DisplayName("Byte 1")]
public string B1
{
get => b1;
set { b1 = value; OnPropertyChanged(); }
}
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
public partial class fmVisualizer : Form
{
BindingList Frames;
public fmVisualizer ()
{
InitializeComponent();
Frames = new BindingList();
formPrincipal.FrameReceived += FrameReceived; //some event will add new data
dgv.DataSource = Frames; //my dataGridView
}
private void FrameReceived(string somePgn)
{
Frame found=Frames.Where(x=>x.PGN==somePgn).FirstOrDefault();
if (found!= null)
{
//...
}
else
{
Frame newFrame = new Frame()
{
PGN = somePgn,
B0 = "something1...",
B1 = "something2..."
};
Frames.Add(newFrame); //THROWS System.InvalidOperationException
}
}
}
Код: Выделить всё
dgv.DataSource = Frames;Подробнее здесь: https://stackoverflow.com/questions/786 ... nexception
Мобильная версия