RadGrid for ASP.NET

No wrap for grid cell content Send comments on this topic.
How-to > No wrap for grid cell content

Glossary Item Box

This appearance customization is not supported out-of-the-box in the current version of Telerik RadGrid. However, there is a workaround which you can apply to prevent the wrapping for column cells content. Set the DataFormatString property for each column to <nobr>{0}</nobr}. Thus the column(s) in your grid instance should stretch to fit the width of the longest text inside the column(s) cells.

For declarative columns the code be as below:

ASPX/ASCX Copy Code
<rad:radgrid id="RadGrid1" runat="server">
  
<MasterTableView AutoGenerateColumns="false">
    
<rad:GridBoundColumn UniqueName="ContactName" HeaderText="Contact name" DataField="ContactName" DataFormatString="<nobr>{0}</nobr>" />
    
<rad:GridBoundColumn UniqueName="ContactTitle" HeaderText="Contact title" DataField="ContactTitle" DataFormatString="<nobr>{0}</nobr>" />
    
<rad:GridBoundColumn UniqueName="CompanyName" HeaderText="Company name" DataField="CompanyName" DataFormatString="<nobr>{0}</nobr>" />
    
<rad:GridBoundColumn UniqueName="Country" HeaderText="Country" DataField="Country" DataFormatString="<nobr>{0}</nobr>" />
    
<rad:GridBoundColumn UniqueName="City" HeaderText="City" DataField="City" DataFormatString="<nobr>{0}</nobr>" />
 
</MasterTableView>
</
rad:radgrid>
 
For auto-generated column subscribe to the ColumnCreated event and set the DataFormatString in the corresponding handler:

C# Copy Code
protected void RadGrid1_ColumnCreated(object sender, GridColumnCreatedEventArgs e)
{
       
//add more column types to the conditional check if needed
       
if (e.Column is GridBoundColumn)
       {
           (e.Column
as GridBoundColumn).DataFormatString = "<nobr>{0}</nobr>";
       }
}

VB.NET Copy Code
Protected Sub RadGrid1_ColumnCreated(ByVal sender as Object, ByVal e as GridColumnCreatedEventArgs) Handles RadGrid1.ColumnCreated
        'add more column types to the conditional check if needed
        If (TypeOf e.Column is GridBoundColumn) Then
            CType(e.Column, GridBoundColumn).DataFormatString = "<nobr>{0}</nobr>"
        EndIf
End Sub

In the code-behind (just databinding):

C# Copy Code
 private void RadGrid1_NeedDataSource(object source, Telerik.WebControls.GridNeedDataSourceEventArgs e)
 {
  OleDbConnection MyOleDbConnection =
new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("~/Grid/Data/Access/Nwind.mdb"));
  OleDbDataAdapter MyOleDbDataAdapter =
new OleDbDataAdapter();
  MyOleDbDataAdapter.SelectCommand =
new OleDbCommand("SELECT TOP 10 CompanyName, ContactName, ContactTitle, Country, City FROM Customers", MyOleDbConnection);
  DataTable myDataTable =
new DataTable();
  MyOleDbConnection.Open();
  
try
  {
   
MyOleDbDataAdapter.Fill(myDataTable);
  }
  
finally
  {
   
MyOleDbConnection. Close();
  }
  RadGrid1.DataSource = myDataTable.DefaultView;
 }
VB.NET Copy Code
Private Sub RadGrid1_NeedDataSource([source] As Object, e As Telerik.WebControls.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
   Dim MyOleDbConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("~/Grid/Data/Access/Nwind.mdb"))
   Dim MyOleDbDataAdapter As New OleDbDataAdapter()
   MyOleDbDataAdapter.SelectCommand = New OleDbCommand("SELECT TOP 10 CompanyName, ContactName, ContactTitle, Country, City FROM Customers", MyOleDbConnection)

   Dim myDataTable As New DataTable()

   MyOleDbConnection.Open()
   Try
      MyOleDbDataAdapter.Fill(myDataTable)
   Finally
      MyOleDbConnection. Close()
   End Try

   RadGrid1.DataSource = myDataTable.DefaultView
End Sub 'RadGrid1_NeedDataSource
Note: This functionality is not supported with static headers (UseStaticHeaders = true) and column resizing. With static headers the cell content will be clipped if you resize a column to make its width dimension smaller than the longest cell data.