title: Making an ASP.Net GridView column a fixed size and do word wrapping
body: <p>In an ASP.Net web form there is a GridView. We want to have a fixed size for 1 of the columns in this GridView and have the text in it also do word wrapping. We can't get it to work.</p>
  
  <p>Here is the markup I have tried:</p>
  
  <pre><code>&lt;asp:BoundField DataField="AssignmentDetails" HeaderText="Assignment" 
      SortExpression="AssignmentDetails" ItemStyle-Width="20" ItemStyle-Wrap="true"&gt;
  
      &lt;HeaderStyle HorizontalAlign="Left" /&gt;
      &lt;ItemStyle HorizontalAlign="Left" /&gt;
  &lt;/asp:BoundField&gt;
  </code></pre>
  
  <p>We thought that this would make the column 20 characters wide and make the words wrap but it doesn't do that.</p>
  
  <p><strong>* Update *</strong></p>
  
  <p>It's now working based on Darren's coding sample. Here is the full markup of the GridView and the code-behind using his technique:</p>
  
  <pre><code>        &lt;asp:GridView
              ID="GridViewSummary" 
              runat="server" 
              AllowSorting="True" 
              AutoGenerateColumns="False" 
              DataKeyNames="ID" 
              Width="691px" 
              AllowPaging="True"
              PageSize="5"
              OnRowDataBound="GridViewSummary_RowDataBound"&gt;
  
              &lt;Columns&gt;
                  &lt;asp:BoundField DataField="AssignmentDate" HeaderText="Date" 
                      SortExpression="AssignmentDate" DataFormatString="{0:MM/dd/yyyy}"&gt;
  
                      &lt;HeaderStyle HorizontalAlign="Left" /&gt;
                      &lt;ItemStyle HorizontalAlign="Left" /&gt;
                  &lt;/asp:BoundField&gt;
  
                  &lt;asp:BoundField DataField="AssignmentDueDate" HeaderText="Date Due" 
                      SortExpression="AssignmentDueDate" DataFormatString="{0:MM/dd/yyyy}"&gt;
  
                      &lt;HeaderStyle HorizontalAlign="Left" /&gt;
                      &lt;ItemStyle HorizontalAlign="Left" /&gt;
                  &lt;/asp:BoundField&gt;
  
                  &lt;asp:BoundField DataField="Class" HeaderText="Class" 
                      SortExpression="Class"&gt;
  
                      &lt;HeaderStyle HorizontalAlign="Left" /&gt;
                      &lt;ItemStyle HorizontalAlign="Left" /&gt;
                  &lt;/asp:BoundField&gt;
  
                  &lt;asp:BoundField DataField="TeacherName" HeaderText="Teacher" 
                      SortExpression="TeacherName"&gt;
  
                      &lt;HeaderStyle HorizontalAlign="Left" /&gt;
                      &lt;ItemStyle HorizontalAlign="Left" /&gt;
                  &lt;/asp:BoundField&gt;
  
                  &lt;asp:TemplateField HeaderText="Assignment" SortExpression="AssignmentDetails"&gt;
                      &lt;ItemTemplate&gt;
                          &lt;asp:Label ID="LabelAssignment" runat="server" Text='&lt;%# Bind("AssignmentDetails") %&gt;'&gt;&lt;/asp:Label&gt;
                      &lt;/ItemTemplate&gt;
  
                      &lt;HeaderStyle HorizontalAlign="Left" /&gt;
                      &lt;ItemStyle HorizontalAlign="Left" Wrap="True" /&gt;
                  &lt;/asp:TemplateField&gt;
  
                  &lt;asp:TemplateField ShowHeader="False"&gt;
                      &lt;ItemTemplate&gt;
                          &lt;asp:Button 
                              ID="ButtonSelect" 
                              runat="server" 
                              CausesValidation="False" 
                              CommandName="Select" 
                              Text="Select Assignment Details" /&gt;
                      &lt;/ItemTemplate&gt;
                  &lt;/asp:TemplateField&gt;
              &lt;/Columns&gt;
          &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>Code-behind:</p>
  
  <pre><code>Protected Sub GridViewSummary_RowDataBound(sender As Object, e As GridViewRowEventArgs)
  
      ' Deal with Data type rows, and not headers etc.
      '-----------------------------------------------
      If (e.Row.RowType = DataControlRowType.DataRow) Then
  
          Dim lblAssignment As Label = e.Row.FindControl("LabelAssignment")
  
          ' Call a recursive method and insert a line break every 20 chars.
          '----------------------------------------------------------------
          lblAssignment.Text = InsertlineBreak(lblAssignment.Text)
      End If
  End Sub
  
  Function InsertlineBreak(ByVal original As String) As String
  
      Dim MaxStringLength As Int16 = 20
  
      If original.Length &gt; MaxStringLength Then
          Dim indexOfSpace = original.IndexOf(" ", MaxStringLength - 1)
          If indexOfSpace &lt;&gt; -1 AndAlso indexOfSpace &lt;&gt; original.Length - 1 Then
              Dim firstString As String = original.Substring(0, indexOfSpace)
              Dim secondString As String = original.Substring(indexOfSpace)
  
              Return firstString &amp; "&lt;br/&gt;" &amp; InsertlineBreak(secondString)
          Else
              Return original
          End If
      Else
          Return original
      End If
  End Function
  </code></pre>
  
  <p>I also applied this technique to our DetailsView as well as shown in this markup and code-behind:</p>
  
  <pre><code>                &lt;asp:TemplateField HeaderText="Details:" SortExpression="AssignmentDetails"&gt;
                      &lt;EditItemTemplate&gt;
                          &lt;asp:TextBox ID="TextBoxAssignmentDetails" runat="server" Text='&lt;%# Bind("AssignmentDetails") %&gt;' TextMode="MultiLine"
                              rows="5"&gt;&lt;/asp:TextBox&gt;
                      &lt;/EditItemTemplate&gt;
  
                      &lt;InsertItemTemplate&gt;
                          &lt;asp:TextBox ID="TextBoxAssignmentDetails" runat="server" Text='&lt;%# Bind("AssignmentDetails") %&gt;' TextMode="MultiLine"
                              rows="5"&gt;&lt;/asp:TextBox&gt;
                      &lt;/InsertItemTemplate&gt;
  
                      &lt;ItemTemplate&gt;
                          &lt;asp:Label 
                              ID="LabelAssignmentDetails" 
                              runat="server" 
                              Text='&lt;%# Bind("AssignmentDetails") %&gt; '
                              OnDataBinding="LabelAssignmentDetails_DataBinding"&gt;
                          &lt;/asp:Label&gt;
                      &lt;/ItemTemplate&gt;
  
                      &lt;ItemStyle ForeColor="Blue" /&gt;
                  &lt;/asp:TemplateField&gt;
  </code></pre>
  
  <p>Code-behind:</p>
  
  <pre><code>Protected Sub LabelAssignmentDetails_DataBinding(sender As Object, e As EventArgs)
  
      Dim lblAssignment As Label = DetailsView.FindControl("LabelAssignmentDetails")
  
      ' Call a recursive method and insert a line break every 20 chars.
      '----------------------------------------------------------------
      lblAssignment.Text = InsertlineBreak(lblAssignment.Text)
  End Sub
  </code></pre>