I work often with data-bound applications with WinForms; records are pulled from a database and updated in a client application that I write. Stored procedures are used for all database transactions in the client.
I need to programmatically refer to columns from the database - for example, to format a dollar value.
resultsTable.Columns[<index>].DefaultCellStyle.Format = "$0.00"
I strongly dislike using hard-coded integer indices.
I've played with enums, like this:
enum Columns
{
ID,
OrderID,
Notes,
CustomerName,
AssembledBy,
Status,
CreateDate
}
But doing this, I always have to make an explicit cast resultsTable.Columns[(int)Columns.CreateDate]
and I have to update the enumeration if the stored procedure changes.
I can reference columns by name resultsTable.Columns["create_date"]
. This is easy to read but seems very brittle.
What's a good way to reference the column I'm looking for? I'd love some dynamic way of getting column indices, but I don't know where to start.