I am quite new to C# and want to know the right way (not wasting any resources) to handle Generic Data.
I have a BindingList that is used to bind dataGridView.DataSource.
Below is the example code that updates Data values: The question is whether it be more efficient to place the "calculating codes" in the getter or the setter.
If there is an approach to this that would be considered a 'best practice' please let me know that too.
public class Data : INotifyPropertyChanged
{
private float number;
public string Code
{
get => Number; // get => Number / 100 which one is more efficient?
set { Number = value / 100; OnPropertyChanged(); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
... where the Data class is used like so:
public partial class Form1 : Form
{
private BindingList<Data> dataList = new BindingList<Data> { new Data { Code = "1" } };
protected override void OnHandleCreated(EventArgs e)
{
// When main form is ready to handle messages the binding happens.
base.OnHandleCreated(e);
myDataGridView.DataSource = dataList;
myDataGridView.Columns["Code"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}
public Form1()
{
InitializeComponent();
}
}