forcing datagrid columns to wrap

M

Mark Roberts

I have a datagrid with several columns - one of which has a large
amount of text to display. I have set the width to be 500px and have
set wrap="true". For most of the data this is working fine. The
problem is when the data doesn't contain any white space - like a
long, fully-qualified filename. The data doesn't wrap and the column
becomes extremely wide. Does anyone know how to force the text to
wrap? Thanks.

Mark
 
K

Ken Cox [Microsoft MVP]

Hi Mark,

I suspect you''re going to have to throw in an HTML line break at some
point.

You can do this with a helper function. You probably want to write something
to insert one every x characters but this should get you started:

Public Function FixLongLine(ByVal strIn) As String
If Len(strIn) > 10 Then
Return Left(strIn, 10) & "<br>" & Mid(strIn, 11)
End If
Return strIn
End Function


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

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
Toronto
 
M

Mark Roberts

Ken,

Thank you very much. I have some work to do to cover some special
cases, but this method seems to be working very well. Thanks again.

Mark


Ken Cox said:
Hi Mark,

I suspect you''re going to have to throw in an HTML line break at some
point.

You can do this with a helper function. You probably want to write something
to insert one every x characters but this should get you started:

Public Function FixLongLine(ByVal strIn) As String
If Len(strIn) > 10 Then
Return Left(strIn, 10) & "<br>" & Mid(strIn, 11)
End If
Return strIn
End Function


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

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
Toronto


Mark Roberts said:
I have a datagrid with several columns - one of which has a large
amount of text to display. I have set the width to be 500px and have
set wrap="true". For most of the data this is working fine. The
problem is when the data doesn't contain any white space - like a
long, fully-qualified filename. The data doesn't wrap and the column
becomes extremely wide. Does anyone know how to force the text to
wrap? Thanks.

Mark
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top