There exist quite a variety of built-in format strings for the various types in the .NET runtime. For integers, decimals, floats, dates, etc things are quite happy as you can build up exactly the format you're looking for quite quickly. But the built-in formatting for boolean values are simply true/false. From a boolean algebra perspective, thats all anyone should ever need. But if you're working with preferences-type information (email a customer when a specific even occurs, does a particular item have a particular feature) boolean values can indicate more of yes/no type data rather than true/false. Sematically they're the same, but in terms of "looking right" yes/no is "more correct"
Currently in order to get this feature in a datagrid I need to do several things:
1. Create a template column with a blank label
<asp:TemplateColumn HeaderText="Preference">
<ItemTemplate >
<ASP:Literal id="myPreferenceInstance" runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
2. Create an event handler that runs for every row in the datagrid which finds the control and inserts the appropriate yes/no string.
public void DoExtraBinding(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
bool somepreference = DBConv.ToBoolean(DataBinder.Eval(e.Item.DataItem, "PreferenceFromDataBaseTable"));
Literal myPreferenceInstance= (Literal)e.Item.FindControl("myPreferenceInstance");
if(somepreference)
myPreferenceInstance.Text = "Yes";
else
myPreferenceInstance.Text = "No";
}
}
While I realize that this is a solution, and that a better one might exist, I haven't been able to find it. What would be really elegant, however, is this:
<Asp:BoundColumn HeaderText="Preference" DataField="PreferenceFromDataBaseTable" DataFormatString="{0:yn}" />
This is of course, assuming that {0:yn} is the particular syntax necessary in order to achieve printing of yes/no rather than true/false based upon the value of the variable.
I did some research into trying to do this myself by writing some custom formatting strings and formatting providers, but from my rather limited understanding of how all that works, it didn't seem like I would be able to actually use the custom formatting string within an ASP.NET page unless it was built into the framework somehow, and I don't know enough about the internals of ASP.NET to do that myself.
I hope this snippet makes it to the right developer at Microsoft who can do something to make our lives out here in the trenches a bit easier.
Thanks,
Mike Sandford