By default, WebDataGrid will change the content area of the grid to its contents if there is no width in the column and the grid itself. Then the headers get the size for the piece of data. This will work well if the mesh is in the container or outside the container.
Since some of the data in the grid is the correct size, all we need to do is size the columns, which is larger, the width of the first cell in the grid, or a dimension to display the full header. The following JavaScript will do the following:
function resizeGrid(grid) { var textWidthChecker = document.getElementById('textWidthChecker'); var columns = grid.get_columns(); var widths = new Array(columns.get_length()); var row = grid.get_rows().get_row(0); var newGridWidth = 0; for (var colIdx = 0; colIdx < columns.get_length(); colIdx++) { var col = columns.get_column(colIdx); textWidthChecker.innerHTML = col.get_headerText(); var cellElement = row.get_cell(colIdx)._element; if (!col.get_hidden()) { if (textWidthChecker.offsetWidth > cellElement.offsetWidth) {
The above requires a range with id "textWidthChecker" for this to work using the same CSS classes as the mesh heading for the font:
<div style="height: 0px;overflow: hidden;" class="igg_HeaderCaption ig_Control"> <span id="textWidthChecker"></span> </div>
This approach works well when the resizeGrid function is called using the client-side initialization method if the grid is inside a container that is wide enough for the grid to resize headers.
If the grid is in a smaller container, this will not be done because WebDataGrid sets the internal width, and this affects the size of the columns. To get around this, the resizeGrid function must be called earlier, although there is no event for this.
In particular, resizeGrid must be called during grid function initialization between calls to _initializeObjects and _adjustGridLayout. To do this, you can modify the WebDataGrid prototype to replace _adjustGridLayout with another one that calls resizeGrid and calls the original _adjustGridLayout:
var adjustGridLayoutFunction = $IG.WebDataGrid.prototype._adjustGridLayout; $IG.WebDataGrid.prototype._originalAdjustGridLayout = adjustGridLayoutFunction; $IG.WebDataGrid.prototype._adjustGridLayout = function () { resizeGrid(this); this._originalAdjustGridLayout(); };
Note that changing the prototype affects every grid on the page, so if you want to control which grid is applicable to you, you will need to add conditional logic to the new _adjustGridLayout function. For instance:
if (this.get_id() == "WebDataGrid1") resizeGrid(this);
To prevent text wrapping inside the grid, you also need to use css for ItemStyle:
.noWrapCellStyle { white-space: nowrap; }
To do this, specify the desired width on the mesh container, and the mesh width should remain empty:
<div style="width:300px;overflow: auto;"> <ig:WebDataGrid ID="WebDataGrid1" runat="server" Height="450px" AutoGenerateColumns="True" ItemCssClass="noWrapCellStyle"> </ig:WebDataGrid> </div>
If you use a row selector, then the calculations should be updated in the logic that I provide for the row selector account.