I am trying to transfer my excel app to WPF datagrid. I am going to enter data to Column A and in column B I would like to make calculation taking previus cell and current cell of A column and add Column B previus cell. calculation example : B2 = B1 + (A2-A1). What is best approach of doing so?
Personally, I'd start by creating a class that represents the records and implement INotifyPropertyChanged on that class.
public class recordObject : INotifyPropertyChanged
{
private int a;
public int A
{
get
{
return a;
}
set
{
a = value;
OnPropertyChanged("A");
}
}
private int b;
public int B
{
get
{
return b;
}
set
{
b = value;
OnPropertyChanged("B");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Then, in your code behind on the window you're showing the datagrid, you'll want to subscribe to PropertyChanged on each object in the list. Then you'd have to manually compute the column values whenever those properties changed. Ick, I know, but it'd work.
The property changed event would look like:
void recordObject_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
var objectList = DataGrid.ItemsSource as List<recordObject>;
var myRecord = sender as recordObject;
if (objectList != null && myRecord != null)
{
int idx = objectList.IndexOf(myRecord);
// Perform your calculations here using idx to access records before and after the current record
// making sure to check for list boundaries for top and bottom.
// Also note that this will likely kick off cascading event calls so make sure you're only changing
// the previous or following record object.
}
}
If you hook this event onto all the records in your bound list, then it'll fire whenever any property is changed. In the class above, that'd apply to both A and B. You can filter which properties you're interested in monitoring through e.PropertyName (a simple string comparison) and guage the business logic accordingly. If you want to maintain encapsulation, or at least, put the business logic for the object on the object itself, this method could be a static one on the class recordObject. You'd have to provide for getting hold of the datagrid from that static method, though (likely through a static property on your window). So:
public static void recordObject_PropertyChanged(object sender, PropertyChangedEventArgs e)
and connected so:
record.PropertyChanged += new PropertyChangedEventHandler(recordObject.recordObject_PropertyChanged);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With