I need to use C# methods to set the values of some DataColumns in a table. DataColumn provides an Expression property, but that only allows a subset of SQL style calculations on the dataset. What I need to do is call a C# method to perform calculations too complex for the Expression syntax and then put the data into a row’s column. I would like that value to be set when the data table is loaded and again whenever the current row is updated, at least.
Is there any way to implement a true calculated field in ADO.NET? When I used Delphi, you could use the designer to add a calculated field to a table and define the event which updates it, and the event would be called as needed. I can’t seem to find an equivalent in ADO.NET.
DataRow, DataRowView and DataColumn have no events of use. I am not allowed to edit data in the DataTable.RowChanging event. If I edit in the RowChanged event it causes an endless loop because you’re changing the row again. The various events of the BindingSource only work on the currently selected record (useful for the second case, at least).
The only solution I have found to date is to call my method in BindingSource.CurrentChanged. That only fills the current record, so I must step through all of the rows in my DataTable immediately after a Fill operation to trigger that event to set the initial values. That’s a bit inefficient when there a lot of rows in the table.
Is there a more efficient way to get a value set?
Jump to Post
Would it perhaps be possible to modify your SQL query to do the work for you?
You could do the initial query, then do a UNION ALL using the query that does the calculations. That way, you would have one single result set, and the last row should be …
All 2 RepliesReply to this topic
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.