AutoResize Not Working Properly When ColumnHeader Is Longest String - c#

dgvSampled.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
I'm trying to size the columns with the AllCells mode. The problem is that when the longest string in the column is the header, a buffer is left over.
Compare the Requested column to the Coil # column. Why does the Coil # column size itself perfectly to its content, but the Requested column sizes itself too large for its header title?
After making the columns unable to be sorted, they are now being sized as I expected them to be in the first place.

From the documentation (emphasis mine):
AllCells: The column widths adjust to fit the contents of all cells in the columns, including header cells.
From your description, it seems that what you want is DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader

Related

How to show a cell value in two cells in a row when the length of the cell value is large in datagridview in c#

In C# Windows Application DataGridView, the value of a cell is a text with more than 40 characters. The characters are not showing when the column size is less. If the column size is increased, then other column values got disturbed. So I want a solution that, the cell value with more characters overlap or display in the next cell(ie.next column). (Like in Excel)
I already tried Wrap mode and autosizecolumnsmode.
I expect an output shown below
(source: fordevs.com)
You can use a custom implementation using
Canvas.SetZIndex(item,index);

How to set data to resize depending on columns and vise versa inside datagridview using C#?

I have one problem. When ever I try to print my datagridview it always splits in half, so what I wanna do is to find a way to set length of column name adapt to data inside the column or to set that data adapts to column name. Its not the best explanation but take a look at the pictures:
This is what I have now:
This is what I want:
So basically I want the name of the column and data inside the rows to be visible but to take as minimum space as they can, depending on what length is bigger, If column name is bigger, adapt to that, if data length is bigger, adapt to that. Any solution for my problem?
Set the following property:
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
See here the documentation for DataGridViewAutoSizeColumnsMode member AllCells:
The column widths adjust to fit the contents of all cells in the
columns, including header cells.

How to set a min width before word-wrap inside an Asp.net Grid View column iif that columns text is at least certain length. ex 50 words

...without knowing how many columns the grid view may have or which columns will contain a lot of text. (Auto resize all other columns)
I have a grid view on a webpage that is generated from a database and it displays different columns in different orders depending on the user or user groups. Typically, it is displaying somewhere between 15 and 30 columns. For some user/user groups all of the columns can fit on the screen without any text wrapping too much, for others even after the grid view auto sizes all of the columns to be as small as possible it doesn’t all fit on the screen requiring a horizontal scroll bar and it’s then forcing the columns with large amount of text to wrap after only one or two words.
I am trying to figure out if there is a way to stop a column from resizing to a tiny size if its content is really long and instead wrap only after a certain size (assuming that the content of that column is large enough to require the column to be that “certain size” to begin with) however I can’t know which column is going to have a ton of words since each user / user group can modify which columns they use, which are displayed, and what each column is called. (So I can’t hard code a specific column to be a minimum/specific width)
Example
If for example a column has text that is long than say, 50 words, I want to set that column to only word wrap after at least 4 or 5 words have already been displayed on the first line (or a certain pixel size) But if a column is smaller than that I want the column to become as small as possible so that as many rows / columns can be visible on the screen as possible. I don’t mind the horizontal scroll but I am trying to get the wrapped text to not wrap more than 5 or so lines. (Currently it’s wrapping like 15-30 lines leaving 1 -2 words on a line)
Example – What I want
UserGroup1
This is what I would like to happen (The column with the long text is kept large enough to read multiple words before wrapping
Example -- What I’m currently getting
UserGroup1 (This only happens to users groups who have a ton of columns, other user groups are able to fit everything on the screen without ridiculous word wrap)
Example of what I'm currently getting -- Row with lots of columns is word wrapping every word or two causing the row to take up ton of space on the site. (Made image a bit smaller since its so tall)
This User/User group also has so many columns set to display that it has a horizontal scroll making it difficult for users to get to easily read the data.
Additional Example
Example of a different user group who has less columns and a different order.
Description
This is an example of a different user/user groups gridview display. This user/user group has chosen to have a lot less columns and all of the columns have no problem fitting on the page. This gridview does not have a horizontal scroll bar and already wraps nicely due to less columns.
This grid view automatically resizes the smaller columns and makes the column with more data bigger because it doesn’t need to try to fit a ton of columns on the page. This example is similar to the "What I want" example, except it has less columns so the description column is wider.

iText. How to adjust header width (C#)?

I need adjust header width of the table created with iText. Column width set with table.SetWidths(lstMaxCount.ToArray()) method, that is standard way to do this, but is there way to adjust column header width to avoid break line?
Here is how it look like now:
I think one solution would be to make the header display on 2 rows for the columns with small width.
My suggestion would be to split PackageNumber column name in two words, and put a new line inside:
"Package" + Environment.NewLine + "Number"
Make your header height enough to fit 2 lines of text in it.
Set the PdfPCell's property NoWrap = false
There are also another options to wrap a text on multiple lines. You can use Paragraphs and Chunks. You can take a look here: http://itextpdf.com/examples/iia.php?id=35enter link description here

EPPlus - AutoFitColumns() method fails when a column has merged cells

I was wondering if anyone has come up with a workaround to this problem. I've noticed that the AutoFitColumns() method is failing on columns with merged cells. I've included a basic code example below:
var cellRange = worksheet.Cells[1, column, 2, column];
cells.Merge = true;
cells.Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
cells.Value = "Some Text Goes Here";
worksheet.Cells.AutoFitColumns();
The resulting worksheet will have the cells in rows 1 and 2 merged (correctly) for the column in the column variable, but that particular cell is disregarded by the AutoFitColumns() method.
Any help would be appreciated.
Basically...
AutoFitColumns is documented to ignore merged cells. It isn't failing, at least in the sense of being defective.
AutoFit within Excel ignores merged cells, too.
Apparently up through Excel 2007, you cannot use the AutoFit feature for rows or columns that contain merged cells at all.
I've only tested with Excel 2013, which seems to behave differently:
Auto-fitting a row ignores all row-merged cells in that row.
Auto-fitting a column ignores all column-merged cells in that column.
In other words, you can use AutoFit in rows and columns with merged cells, it'll just ignore any cells that have been merged along the same dimension you're auto-fitting.
Desired effect of AutoFit w/r/t merged cells? (+ workarounds)
Finally, I'm not sure whether I see how it makes sense to auto-fit with respect to a merged cell. For example, suppose you have a merged cell at A1:B1 with content that fills a default two-column width.
If you auto-fit on column A, what is supposed to happen? Is column A supposed to become wide enough to fit all of A1:B1, sort of like treating the merged cell as if it's content existed only in A1, the top-left original cell? That might be reasonable, insofar as I can't immediately see whether strange behaviors might be implied in some circumstances.
If something like that is desired, I'd insert the content, auto-fit, and only then merge.
But what if you want to auto-fit on column B, in a situation like this:
Here, you might want column B to get wide enough so that all of the content in A1:B1 shows. (The content is just the text "well hello world".)
There's no one-liner for this, I'm afraid. Here's one way to do it, though:
Insert the content in the as-yet-unmerged top-left cell.
Save the current width of this cell's column.
Auto-fit the column.
Save the new column width.
Reset the column width to what you saved in step 2.
Merge the cells you want to merge.
Total up the widths of all your merged columns but the last one.
Subtract this total from the width you saved in step 4.
Set the last merged column's width to the result of step 8.
More generally, you can split up the total auto-fitted width from step 4 among the merged columns in any way you see fit.
To do all this, you'll want to use ExcelColumn.AutoFit and ExcelColumn.Width (and ExcelWorksheet.Column to grab the ExcelColumn objects).
But most simply...
If your content is static (or at least dynamic but not too variable in length), you can simply set reasonable fixed width(s) for the column(s) in question, distributed however you'd like.

Categories