Rendering proper case/title case on bound data in datagrid?

Discussion in 'ASP .Net Web Controls' started by Guest, Dec 22, 2004.

  1. Guest

    Guest Guest

    ASP.NET newbie, here. I'm binding data to a datagrid. I want one of my
    fields rendered in proper case AKA Title Case. What's the easiest way to
    make that happen?

    More generally, I'm interested in the best ways to impose a string
    formatting function on data bound to a datagrid.

    Thanks.

    -KF
     
    Guest, Dec 22, 2004
    #1
    1. Advertising

  2. Hi Ken,

    In this case, I'd use a helper function and some nice built-in
    functionality.

    Pass your raw string into the helper function, process it and display the
    changed version.

    I've inserted a sample below that should get you going.

    Let us know if this helps?

    Ken
    Microsoft MVP [ASP.NET]
    Toronto


    <asp:datagrid id="DataGrid1" runat="server"
    autogeneratecolumns="False">
    <columns>
    <asp:templatecolumn HeaderText="StringValue">
    <itemtemplate>
    <asp:Label runat="server" Text='<%#
    MakeTitleCase( DataBinder.Eval(Container, "DataItem.StringValue")) %>'>
    </asp:label>
    </itemtemplate>
    </asp:templatecolumn>
    </columns>
    </asp:datagrid>


    Private Sub Page_Load _
    (ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
    If Not IsPostBack Then
    DataGrid1.DataSource = CreateDataSource()
    DataGrid1.DataBind()
    End If
    End Sub

    Function MakeTitleCase _
    (ByVal strIn As String) As String
    Dim myTI As System.Globalization.TextInfo = _
    New System.Globalization.CultureInfo("en-CA", False).TextInfo
    Return myTI.ToTitleCase(strIn)

    End Function
    Function CreateDataSource() As DataTable
    Dim dt As New DataTable
    Dim dr As DataRow
    dt.Columns.Add(New DataColumn _
    ("IntegerValue", GetType(Int32)))
    dt.Columns.Add(New DataColumn _
    ("StringValue", GetType(String)))
    dt.Columns.Add(New DataColumn _
    ("CurrencyValue", GetType(Double)))
    dt.Columns.Add(New DataColumn _
    ("Boolean", GetType(Boolean)))
    Dim i As Integer
    For i = 0 To 8
    dr = dt.NewRow()
    dr(0) = i
    dr(1) = "george smith" + i.ToString()
    dr(2) = 1.23 * (i + 1)
    dr(3) = (i = 4)
    dt.Rows.Add(dr)
    Next i
    Return dt
    End Function


    http://msdn.microsoft.com/library/d...lobalizationtextinfoclasstotitlecasetopic.asp



    <> wrote in message
    news:O$...
    > ASP.NET newbie, here. I'm binding data to a datagrid. I want one of my
    > fields rendered in proper case AKA Title Case. What's the easiest way to
    > make that happen?
    >
    > More generally, I'm interested in the best ways to impose a string
    > formatting function on data bound to a datagrid.
    >
    > Thanks.
    >
    > -KF
    >
    >
     
    Ken Cox [Microsoft MVP], Dec 25, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Klemt
    Replies:
    1
    Views:
    465
    Steve C. Orr, MCSD
    Aug 10, 2003
  2. sunil panda

    Lower bound & Upper bound

    sunil panda, Dec 25, 2003, in forum: Java
    Replies:
    9
    Views:
    7,252
    thushara wijeratna
    Oct 7, 2008
  3. Rhiner Dan
    Replies:
    1
    Views:
    748
    Mike Wahler
    Mar 27, 2005
  4. Mario Krsnic
    Replies:
    0
    Views:
    382
    Mario Krsnic
    Jun 23, 2006
  5. Phillip Vong
    Replies:
    0
    Views:
    415
    Phillip Vong
    Jul 27, 2006
Loading...

Share This Page