Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

BUG: DataGrid Web Server Control Wraps When ItemStyle Wrap or HeaderStyle Wrap Property Is Set to False in Visual C# .NET


View products that this article applies to.

Symptoms

When you set the HeaderStyle Wrap or the ItemStyle Wrap property to False, data is still wrapped in the columns of the DataGrid Web control.

↑ Back to the top


Cause

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.

↑ Back to the top


Resolution

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:
  1. Follow these steps to create a new ASP.NET Web application:
    1. Start Microsoft Visual Studio .NET.
    2. On the File menu, point to New, and then click Project.
    3. In the New Project dialog box, click Visual C# Projects under Project Types, and then click ASP.NET Web Application under Templates.
    4. 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:
      http://localhost/DataGridApp
  2. In Solution Explorer, right-click WebForm1.aspx, click Rename, and then type WrapTest.aspx.
  3. In Design view, drag a DataGrid Web control from the toolbox to the Web form.
  4. In Solution Explorer, right-click the WrapTest.aspx file, and then click View Code.
  5. Add the following code in the WrapTest.aspx.cs file just before "namespace DataGridApp":
    using System.Data.SqlClient;
    					
  6. Add the following code to the Page_Load event handler:
    private void Page_Load(object sender, System.EventArgs e)
    {
        if (!Page.IsPostBack) 
        {
    	SqlConnection conn = new SqlConnection("server=localhost;User ID=<User ID>;PWD=<Password>;Initial Catalog=northwind;");
    	SqlDataAdapter da = new SqlDataAdapter("select * from customers", conn);
    	DataSet ds = new DataSet();
    	da.Fill(ds, "customers");
    	DataGrid1.DataSource = ds;
    	DataGrid1.DataBind();
    	conn.Close();
        }
    }
    						
    NOTE: You must modify the connection string as appropriate for your system database settings.
  7. In Solution Explorer, right-click the WrapTest.aspx file, and then click View Designer.
  8. Right-click the DataGrid control, and then click Property Builder.
  9. Click Columns in the left pane, and then click to clear the Create columns automatically at run time check box.
  10. Follow these steps to add each of the columns explicitly:
    1. In the Available Columns list, click Bound Column, and then transfer this column to the Selected Columns list in the right pane.
    2. Type Customer ID in the Header text box, and then type CustomerID in the Data Field box.
    3. 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.
    4. 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.
    5. 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">
          <Columns>
      	<asp:BoundColumn DataField="CustomerID" HeaderText="Customer ID">
      	    <ItemStyle Wrap="False"></ItemStyle>
      	</asp:BoundColumn>
      	<asp:BoundColumn DataField="CompanyName" HeaderText="Company Name">
      	    <ItemStyle Wrap="False"></ItemStyle>
      	</asp:BoundColumn>
      	<asp:BoundColumn DataField="ContactName" HeaderText="Contact Name">
      	    <ItemStyle Wrap="False"></ItemStyle>
      	</asp:BoundColumn>
              <asp:BoundColumn DataField="ContactTitle" HeaderText="Contact Title">
      	    <ItemStyle Wrap="False"></ItemStyle>
      	</asp:BoundColumn>
      	<asp:BoundColumn DataField="Address" HeaderText="Address">
      	    <ItemStyle Wrap="False"></ItemStyle>
      	</asp:BoundColumn>
      	<asp:BoundColumn DataField="City" HeaderText="City">
      	    <ItemStyle Wrap="False"></ItemStyle>
      	</asp:BoundColumn>
          </Columns>
      </asp:datagrid>
      						
  11. Save all files.
  12. Build the solution.
  13. View the WrapTest.aspx page in the browser. Notice that the text in the columns of the DataGrid does not wrap.

↑ Back to the top


Status

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

↑ Back to the top


References

For additional information about data binding in ASP.NET, click the article number below to view the article in the Microsoft Knowledge Base:
307860 INFO: ASP.NET Data Binding Overview
For more information about data binding in ASP.NET, visit the following Microsoft Web site:

↑ Back to the top


Keywords: KB324165, kbservercontrols, kbpending, kbdatabinding, kbbug, kbarchive, kbnosurvey

↑ Back to the top

Article Info
Article ID : 324165
Revision : 8
Created on : 2/27/2014
Published on : 2/27/2014
Exists online : False
Views : 329