Adding Custom Fields To BO Datatable


Author
Message
Tim Dol
Tim Dol
StrataFrame User (434 reputation)StrataFrame User (434 reputation)StrataFrame User (434 reputation)StrataFrame User (434 reputation)StrataFrame User (434 reputation)StrataFrame User (434 reputation)StrataFrame User (434 reputation)StrataFrame User (434 reputation)StrataFrame User (434 reputation)
Group: Forum Members
Posts: 340, Visits: 1.4K
I've seen a forum post on this before but can't seem to locate it.



I want to add a few temporary fields to a business object's datatable on a row by row basis to store status information but can't seem to get it to work properly at runtime. I don't want to add the fields to the database table, just to the business object.



Any help would be appreciated.



Thanks,

Tim

Tim Dol
Tim Dol
StrataFrame User (434 reputation)StrataFrame User (434 reputation)StrataFrame User (434 reputation)StrataFrame User (434 reputation)StrataFrame User (434 reputation)StrataFrame User (434 reputation)StrataFrame User (434 reputation)StrataFrame User (434 reputation)StrataFrame User (434 reputation)
Group: Forum Members
Posts: 340, Visits: 1.4K
I should add that I DO NOT want these fields to show up when I bind the business object to a grid. They should stay hidden.
Dustin Taylor
Dustin Taylor
StrataFrame Team Member (660 reputation)
Group: StrataFrame Users
Posts: 364, Visits: 771
Sounds like you want to add a column to your BO but not expose it using a strong-typed property. You have two options here:

1) Return the column in your SQL query that fills the BO

2) Stuff the values into the BO in .NET after it gets filled

First Option

If those fields would be present in your query, you can just include them in it and they will be accessible on the row, but not exposed through a strong-typed property.

So, if you have a "Customer" BO with "cs_FirstName", "cs_LastName", and "cs_ID", but want to add a calculated status column of "cs_Status", then you could formulate your SQL query like this:

SELECT
    cs_FirstName,
    cs_LastName,
    cs_ID,
    (your calculation query here) AS cs_Status
FROM Customers

Once you use that to fill your BO, you can access that status column by referring to the current row:

CType(MyCustomerBO.CurrentRow.Item("cs_Status"), String)

Note that since it isn't strong typed, you will probably want to specify the type when you reference that column by using a CType.

Second Option

For the second option, the cleanest thing would probably be to create a method to add the values for you. The method is basically going to take the internal datatable of the business object, add a column, and then populate that column for each item in the BO. Again you'll want to do this after the BO gets populated:

Private Sub CalculateStatus(ByRef dt As DataTable)
   '-- Establish Locals
   Dim MyStatus As String = ""

   '-- Add column
   dt.Columns.Add(New DataColumn("cs_Status))

   '-- Iterate through the rows and set the status
   For Each row As DataRow In dt.Rows
      MyStatus = *Your Status Calculation Here*
      row.Item("cs_Status") = MyStatus
   Next
End Sub

You would then call that method by passing over the datatable:

CalculateStatus(MyCustomersBO.CurrentDataTable)

And would reference the resulting status item in the same way as if you had passed the value back from SQL:

CType(MyCustomerBO.CurrentRow.Item("cs_Status"), String)

Edhy Rijo
E
StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)
Group: StrataFrame Users
Posts: 2.4K, Visits: 23K
Hi Dustin,



Very good explanation, thanks!



BTW, glad to see you back in town Tongue

Edhy Rijo

Tim Dol
Tim Dol
StrataFrame User (434 reputation)StrataFrame User (434 reputation)StrataFrame User (434 reputation)StrataFrame User (434 reputation)StrataFrame User (434 reputation)StrataFrame User (434 reputation)StrataFrame User (434 reputation)StrataFrame User (434 reputation)StrataFrame User (434 reputation)
Group: Forum Members
Posts: 340, Visits: 1.4K
Dustin,



The second option is close. I want to add the column to the datatable and be able to populate it like any other field on the business object, except it won't get saved. I don't need to populate the new field after filling the object.



Example: I want to add a boolean field to the internal datatable called 'cs_Processed'.



I want to be able to set the field cs_Processed = True as I process the rows somewhere in a update routine. After the update is done, I want to loop through the business object and check to see what rows were not processed correctly, cs_Processed = False.



Hope that helps clarify.



Thanks,

Tim

Edhy Rijo
E
StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)
Group: StrataFrame Users
Posts: 2.4K, Visits: 23K
Hi Tim,



You can have your cs_Processed field in the table and it will be created in the BO, then you can add cs_Processed to the BO.FieldsToExcludeFromUpdate property so it won't be updated.



Or as Dustin suggested, you can have a Custom Field Property in your BO, then use it normaly as any other field, and no data will be save back since it is not binded to anything.



Other suggestion is that if you allow your users to set the cs_Processed flag, then you can use a ListView with its CheckBoxes property True and then this will add a checkbox on the first column where you can check and then you can use the ListView.CheckedItems.Count > 0 to see which one have been checked and process your records.

Edhy Rijo

Dustin Taylor
Dustin Taylor
StrataFrame Team Member (660 reputation)
Group: StrataFrame Users
Posts: 364, Visits: 771
Yep, what Edhy said Smile

If you explicitly don't want a strong-typed property, then you can use the methods I described in my original post, and reference bo.CurrentRow.Item("yourcolumname") to get or set it. Since it isn't explicitely defined in the BO, it won't save back to the database. If you do want to use a strong-typed property (which is generally much cleaner), then you'll want to add it to the FieldsToExcludeFromUpdate as Edhy mentioned to prevent it from saving back to the database.

Thanks Edhy. Sadly, the Mexican Circus got another Strong Man and weren't interested in a Bearded Lady, so I had to come back Wink.

Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.4K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
Tim,



One last thing that might make life easier. You can use your query that fills the BO to make that extra column. I.e. extending the example



SELECT

-- First three columns are all "official" columns and will be saved by the BO

cs_FirstName,

cs_LastName,

cs_ID,

-- Any number of "extra" columns can be added as needed.

FALSE AS cs_Processed, -- this adds a column to underlying datatable with a default value

0 AS cs_Status -- Example of a couple more added columns, an int and a string

'P' As cs_StatusDisplay

FROM Customers



Using this method, you can then use either of the methods Dustin described to access/change the data.
Tim Dol
Tim Dol
StrataFrame User (434 reputation)StrataFrame User (434 reputation)StrataFrame User (434 reputation)StrataFrame User (434 reputation)StrataFrame User (434 reputation)StrataFrame User (434 reputation)StrataFrame User (434 reputation)StrataFrame User (434 reputation)StrataFrame User (434 reputation)
Group: Forum Members
Posts: 340, Visits: 1.4K
Thanks guys, your examples helped solve my problem.
Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.4K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
Glad you got it working!
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search