I have a ton of data that I want to present in a dataGridView. The columns change from time to time. So I am using a List<Class> datasource to bind to the datagridview to help manage all of this, and so far that is working well.
But I am wondering if there is a better way to structure the class itself for ease in accessing the data it contains.
I have numeric data that needs to be stored as numeric data but presented as formatted strings in the control. So I have set up private fields for storing the numeric data and created set{fieldname}(foo) and get{fieldname}() methods to access the data in the private fields. Then I link the public properties to formatted strings derived from the private data.
Is that a smart way to do it? Is there a better way? I am a bit of a noob with C# and trying to learn best practices or at least better practices.
Here is a test example of the kind of class that I might bind to the control as a list:
Code:
public class clsTest
{
// Private data that isn't part of the datasource
private String FNameValue = String.Empty;
private Decimal SignBonusValue = 0;
private int SalaryTypeValue = 1;
private Decimal SalaryValue = Convert.ToDecimal(50000);
// Methods to assign values to private data
public void setSignBonus(Decimal v)
{
this.SignBonusValue = v;
}
public void setSalaryType(int v)
{
this.SalaryTypeValue = v;
}
public void setSalary(Decimal v)
{
this.SalaryValue = v;
}
// Methods to return values from private data
public Decimal getSignBonus()
{
return this.SignBonusValue;
}
public int getSalaryType()
{
return this.SalaryTypeValue;
}
public Decimal setSalary()
{
return this.SalaryValue;
}
// Public properties that are used by the datasource:
public String FName => this.FNameValue;
public String SignBonus => String.Format("${0,10:#,##0.00}", this.SignBonusValue);
public String Salary => this.SetItemString(this.SalaryTypeValue, this.SalaryValue);
public clsTest(String s)
{
this.FNameValue = s;
this.SalaryValue=0;
}
private String SetItemString(int v, Decimal o)
{
String Item = String.Empty;
switch (v)
{
case 1:
Item = "Base";
break;
case 2:
Item = "Commission";
break;
}
return String.Format("{0} = {1:C2}", Item, o);
}
}
Then I would call something like this to populate some data:
Code:
void Test_Person()
{
Test.AddNew("John");
Test.AddNew("Linda");
Test.AddNew("Rhonda");
this.TestBindingSource.ResetBindings(true);
Decimal v = Convert.ToDecimal(23.05);
Test.TestList[1].setSignBonus(v);
Test.TestList[1].setSalaryType(2);
Test.TestList[1].setSalary(Convert.ToDecimal(50000));
}