Microsoft KB Archive/324165

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

Article ID: 324165

Article Last Modified on 3/21/2003

-

APPLIES TO


 * Microsoft ASP.NET 1.0
 * Microsoft ASP.NET 1.1
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition

-



This article was previously published under Q324165



For a Microsoft Visual Basic .NET version of this article, see 323169.

This article refers to the following Microsoft .NET Framework Class Library namespace:
 * System.Data.SqlClient



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.



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.



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:  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 C# 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:

http://localhost/DataGridApp

</li></ol> </li> In Solution Explorer, right-click WebForm1.aspx, click Rename, and then type WrapTest.aspx .</li> In Design view, drag a DataGrid Web control from the toolbox to the Web form.</li> In Solution Explorer, right-click the WrapTest.aspx file, and then click View Code.</li>  Add the following code in the WrapTest.aspx.cs file just before &quot;namespace DataGridApp&quot;: using System.Data.SqlClient; </li>  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(&quot;server=localhost;User ID=<User ID>;PWD=<Password>;Initial Catalog=northwind;&quot;); SqlDataAdapter da = new SqlDataAdapter(&quot;select * from customers&quot;, conn); DataSet ds = new DataSet; da.Fill(ds, &quot;customers&quot;); DataGrid1.DataSource = ds; DataGrid1.DataBind; conn.Close; } } NOTE: You must modify the connection string as appropriate for your system database settings. </li> In Solution Explorer, right-click the WrapTest.aspx file, and then click View Designer.</li> Right-click the DataGrid control, and then click Property Builder.</li> Click Columns in the left pane, and then click to clear the Create columns automatically at run time check box.</li> Follow these steps to add each of the columns explicitly: <ol style="list-style-type: lower-alpha;"> In the Available Columns list, click Bound Column, and then transfer this column to the Selected Columns list in the right pane.</li> Type Customer ID in the Header text box, and then type CustomerID in the Data Field box.</li> 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.</li> 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.</li>  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=&quot;DataGrid1&quot; runat=&quot;server&quot; AutoGenerateColumns=&quot;False&quot;> <Columns> <asp:BoundColumn DataField=&quot;CustomerID&quot; HeaderText=&quot;Customer ID&quot;> </ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField=&quot;CompanyName&quot; HeaderText=&quot;Company Name&quot;> <ItemStyle Wrap=&quot;False&quot;></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField=&quot;ContactName&quot; HeaderText=&quot;Contact Name&quot;> <ItemStyle Wrap=&quot;False&quot;></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField=&quot;ContactTitle&quot; HeaderText=&quot;Contact Title&quot;> <ItemStyle Wrap=&quot;False&quot;></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField=&quot;Address&quot; HeaderText=&quot;Address&quot;> <ItemStyle Wrap=&quot;False&quot;></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField=&quot;City&quot; HeaderText=&quot;City&quot;> <ItemStyle Wrap=&quot;False&quot;></ItemStyle> </asp:BoundColumn> </Columns> </asp:datagrid> </li></ol> </li> <li>Save all files.</li> <li>Build the solution.</li> <li>View the WrapTest.aspx page in the browser. Notice that the text in the columns of the DataGrid does not wrap.</li></ol>

<div class="status_section">

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

<div class="references_section">