I have data in the following format. The years are dynamic which means after 1994 there can be n more columns.
Block Block Name Area Sown 1989 1990 1991 1992 1993 1994
1 5 losess 10.1 1000.1 702 1003 1002 650 1000
2 4 losess 100 !!! 1002 1003 1002 500 1000
3 2 losess 1000 1000 1002 1003 1002 1000 1000
4 One loss 10000 1000 1002 1003 1002 1000 1000
This data has to be shown on a datagrid in wpf. This data would generally be in a csv file which i would import and show on the grid.
The is a class called Block which defines one row of data.
public class Block
{
public ObservableCollection<DataCell> Yields
{
get;
set;
}
public Block() { }
public Block(ObservableCollection<DataCell> values)
{
Yields = values;
}
}
The datacell is defined as
public class DataCell
{
string name;
object _value;
public string Name
{
get { return name; }
private set { name = value; }
}
public object Value
{
get { return _value; }
set
{
if (value.ToString().Contains('!'))
throw new ArgumentException("Special Characters not allowed");
else if (String.IsNullOrEmpty(value.ToString()))
throw new ArgumentException("Cannot contain blanks.");
else
_value = value;
}
}
public DataCell() { }
public DataCell(string name, object value)
{
Name = name;
Value = value;
}
}
var records = new ObservableCollection<Block>();
var columns = records.First()
.Yields
.Select((x, i) => new { Name = x.Name, Index = i })
.ToArray();
foreach (var column in columns)
{
var binding = new Binding(string.Format("Yields[{0}]", column.Index));
dataGrid.Columns.Add(new CustomBoundColumn()
{
Header = column.Name,
Binding = binding,
TemplateName = "CustomTemplate"
});
}
CustomTemplate is used to display the error on the textbox tooltip.
The issue is that when i import the data file, in the DataCell class the ApplicationException is thrown which makes the application stop at that point and i cannot proceed.
IF i use debug mode and manually run the program and use run to cursor to proceed then the error does not show in the datagrid, but after import is done then if i edit an element the error gets displayed.
Please suggest what i am doing wrong or any better way of importing the above data and showing in the datagrid.