INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Extensability

Formating a datagrid colum at run-time by Zarcom
Posted: 21 Oct 02 (Edited 2 Jun 03)

I have noticed a few people looking for ways to easily to certain things with the datagrid, whether that is formating or some other code.  There is of course the Item Created event for the datagrid, however this may not be what you are looking for.

Another option is what I will be describing here.  Basically you make a function and call that function in the html part of your code.

A simple example would be to format a column from your database so that even if it is null a value is displayed.

Add this function to your code behind page.
Protected Function FormatColumn(ByVal arg as Object) As String
     dim output as String
     If arg Is Nothing Then
          output = "0"
     ElseIf IsDBNull(arg) Then
          output = "0"
     Else
          output = arg
     End If

     FormatColumn = output
End Function


Now in the Html view of your page find the datagrid and column you wish to use the function for.  Replace the line: <asp:TextBox id=txtValue runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.DataField") %>'>
with this one.  <asp:TextBox id=txtValue runat="server" Text='<%# FormatStats(DataBinder.Eval(Container, "DataItem.DataField")) %>'>

Of course you will need to modify the lines slightly to match what you have.  The function itself can be modified to include any conditions that you need.

If you only require some very simple formatting options then the following may be of better use.

If you don't require any complex formatting you may be able to use a built in method.  Although I am talking about the datagrid in this FAQ this both of the formatting methods I am using here will work anytime you use databinding in your web form.

Rather than creating your own formatting method here you can instead add a formatting string onto the end of the databinding call.  The strings that I have found in VS are as follows:
General -    {0}

Dates
---------------------------------
Short Time -    {0:t}
Long Time -    {0:T}
Short Date -    {0:d}
Long Date -    {0:D}
Date & Time -    {0:g}
Full Date -    {0:G}

Numbers
--------------------------------
Number -    {0:N}
Decimal -    {0:D}
Fixed Point -    {0:F}
Currency -    {0:C}
Scientific -    {0:E}
Hexadecimal -    0x{0:X}

The normal databinding syntax as mentioned above looks something like this
<%# DataBinder.Eval(Container, "DataItem.LastNight") %>
  
To add the formatting string just add a comma and put one of the above formatting strings in quotes.
<%# DataBinder.Eval(Container, "DataItem.LastNight", "{0:D}") %>
This would format the incoming data to a long date.  

Just remember that whatever data is incoming must be of the proper type to be formatting by the specified string.  For example if you have a string incoming don't try to add the "{0:C}" formatting string.  The incoming data will not be able to be cast as currency and your application will crash.

Hopefully this will help some people.

Back to Microsoft: ASP.NET FAQ Index
Back to Microsoft: ASP.NET Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close