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

G

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
 
K

Ken Cox [Microsoft MVP]

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top