Improve company productivity with a Business Account.Sign Up


.net datagrid autoGenerateColumns=true words wrap, how to prevent?

Posted on 2004-03-31
Medium Priority
Last Modified: 2012-08-13


I am trying to prevent the words from wrapping in the DataGrid cells when the datagrid is autogenerating the columns.  I have used "ItemStyle-Wrap=False AlternatingItemStyle-Wrap=False" of the datagrid tag and "Wrap=False" of the ItemStyle tag.  Nothing works!  

Any help would be great!
Question by:ee_id
  • 2
LVL 10

Accepted Solution

Kavar earned 2000 total points
ID: 10728237
its a bug...

To work around this problem, make sure that every column of the DataGrid has the ItemStyle Wrap property explicitly set to False as follows:
<ItemStyle Wrap="False"></ItemStyle>
You must create the DataGrid Web control such that the data-bound columns of the DataGrid are added individually. For each column, click to clear the Wrap text within cell check box for its header, its footer, and its item objects as appropriate. To do this, follow these steps:
Follow these steps to create a new ASP.NET Web application:
Start Microsoft Visual Studio .NET.
On the File menu, point to New, and then click Project.
In the New Project dialog box, click Visual Basic Projects under Project Types, and then click ASP.NET Web Application under Templates.
In the Location box, replace the WebApplication# default name with DataGridApp. If you are using the local server, you can leave the server name as http://localhost. The resulting Location box appears as follows:

In Solution Explorer, right-click WebForm1.aspx, click Rename, and then type WrapTest.aspx.
In Design view, drag a DataGrid Web control from the toolbox to the Web form.
In Solution Explorer, right-click the WrapTest.aspx file, and then click View Code.
Add the following code in the WrapTest.aspx.vb file just before "namespace DataGridApp":
Imports System.Data.SqlClient
Add the following code to the Page_Load event handler:
 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Add user code to initialize the page here.
        If Not IsPostBack Then
            Dim conn As New SqlConnection("server=localhost;User ID=sa;Password=password;Initial Catalog=northwind;")
            Dim da As New SqlDataAdapter("select * from customers", conn)
            Dim ds As New DataSet()
            da.Fill(ds, "customers")
            DataGrid1.DataSource = ds
            ds = Nothing
            conn = Nothing
        End If
    End Sub
NOTE: You must modify the connection string as appropriate for your system database settings.
In Solution Explorer, right-click the WrapTest.aspx file, and then click View Designer.
Right-click the DataGrid control, and then click Property Builder.
Click Columns in the left pane, and then click to clear the Create columns automatically at run time check box.
Follow these steps to add each of the columns explicitly:
In the Available Columns list, click Bound Column, and then transfer this column to the Selected Columns list in the right pane.
Type Customer ID in the Header text box, and then type CustomerID in the Data Field box.
Click Format in the left pane. Under Objects, expand Columns and Column[0], and then select the objects (header, items, footer) that you want to disable text wrapping for. Click to clear the Wrap text within cell check box, and then click Apply.
Repeat steps 10a through 10c to create other data-bound columns in succession. In the Data Field box, type CompanyName, ContactName, ContactTitle, Address, City, and so on. Make sure that you click to clear the Wrap text within cell check box for each column.
Click OK in the DataGrid1 Properties dialog box.

NOTE: The column names under the Format functionality appear as Column[1], Column[2], and so on when you add more columns. The final HTML code for the DataGrid1 control appears as follows:
<asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False">
      <asp:BoundColumn DataField="CustomerID" HeaderText="Customer ID">
          <ItemStyle Wrap="False"></ItemStyle>
      <asp:BoundColumn DataField="CompanyName" HeaderText="Company Name">
          <ItemStyle Wrap="False"></ItemStyle>
      <asp:BoundColumn DataField="ContactTitle" HeaderText="Contact Title">
          <ItemStyle Wrap="False"></ItemStyle>
      <asp:BoundColumn DataField="Address" HeaderText="Address">
          <ItemStyle Wrap="False"></ItemStyle>
      <asp:BoundColumn DataField="City" HeaderText="City">
          <ItemStyle Wrap="False"></ItemStyle>
Save all files.
Build the solution.
View the WrapTest.aspx page in the browser. Notice that the text in the columns of the DataGrid does not wrap.

LVL 10

Expert Comment

ID: 10728243
The wrap functionality occurs for each cell and not for each row of the DataGrid. Therefore, if you set the wrap functionality for all of the DataGrid, text wrapping functionality is not disabled for every row or column

Author Comment

ID: 10729137

Thanks very much for the work around, nice to know it is a bug!

Expert Comment

ID: 11852674
For posterity:

If you are dynamincally building your datagrid (number of columns and their names might be different), this is how you'd do the workaround:

    foreach(DataGridColumn dgc in DataGrid1.Columns)
        dgc.ItemStyle.Wrap = false;

Note that you would want to add this code after the DataBind().



Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

In this post we will learn different types of Android Layout and some basics of an Android App.
Why WooCommerce is one of the majorly favored choices when it comes to having an eCommerce store. This article will acquaint you with some reasons that I believe make it one of the best eCommerce platforms available.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

589 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question