Dynamic titles for datagrid columns

P

Patrick

I need to build a multi-lingual application with asp.net. I do not want
to have a version of the application per language used. To achieve
this, I have recorded in my database all the labels and messages of my
application. It worked fine except that if I do my data binding at run
time and display dynamically the datagrid column header according to
the language selected, I loose all the features of the datagrid such as
sorting, paging, etc...

Should you have an idea to solve the problem in a way or another,
please let me know.

Best Regards

Patrick
 
G

Guest

The norm for globalization and localization is resource files. I am not sure
how to do this with a DataGrid, but I am sure it has been tackled somewhere.

I this is not an option, you can always use the ItemDataBound event for the
DataGrid. Here is a small example:

VB.NET
--------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim ht As New Hashtable
ht.Add("0", "something")
ht.Add("1", "something else")

DataGrid1.DataSource = ht
DataGrid1.DataBind()
End Sub

Private Function GetHeader(ByVal x As Integer) As String
'Sample Only: Needs implementation that uses globalization
If (x = 0) Then
Return "ID"
Else
Return "Name"
End If
End Function

Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As _
System.Web.UI.WebControls.DataGridItemEventArgs) Handles _
DataGrid1.ItemDataBound
If (e.Item.ItemType = ListItemType.Header) Then
For counter As Integer = 0 To e.Item.Cells.Count - 1
e.Item.Cells(counter).Text = GetHeader(counter)
Next counter
End If
End Sub

Assumes the following DataGrid:

<asp:DataGrid id="DataGrid1" runat="server" BorderColor="#CC9966"
BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="4"
AutoGenerateColumns="False">
<FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
<SelectedItemStyle Font-Bold="True" ForeColor="#663399"
BackColor="#FFCC66"></SelectedItemStyle>
<ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#FFFFCC"
BackColor="#990000"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="key" HeaderText="Header
Text"></asp:BoundColumn>
<asp:BoundColumn DataField="value" HeaderText="Header Text
2"></asp:BoundColumn>
</Columns>
<PagerStyle HorizontalAlign="Center" ForeColor="#330099"
BackColor="#FFFFCC"></PagerStyle>
</asp:DataGrid>

Yes, it is inanely simple, but it illustrates how to hijack the form if you
cannot find a declarative way to accomplish globalization.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top