This is a simple solution for a really common problem. I assume that most people do the same as as most other programmers and have a common shared library of code between all their project that they add to and use the components as required.
The problem with a database and a bit field when bound to a gridview normally is not very end user friendly. So to make it end user friendly people tend to either use a case statement inside sql to turn the "1" or "0" into a "Yes" or "No". Or they repeat the same code all over the place in multiple projects using the datarow bound event handler where they use the bit fields.
Here is an alternative solution. In your shared project lib. Add the following
class (adding the correct namespace etc..)
public class BoundYesNo : BoundField
{
protected override string FormatDataValue(object dataValue, bool encode)
{
try
{
bool tmp = (bool)dataValue;
if (tmp)
return "Yes";
return "No";
}
catch (Exception ex)
{
return ex.Message;
}
}
}
When you then add the reference to your web config you can then use the above code just like any other bound field on a gridview in asp.net. Now you have something in all your projects that only require you to write asp code and less c#
Did You find this page useful?
Yes
No