ListView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
Listview1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent)
I'm having an issue since both of these lines separately produce the same result, which is resizing both the ColumnContent and the HeaderSize simultaneously.
I've searched and come up with no answer for this, i only need to resize by the HeaderSize but with the above example it seems not to be working correctly... or am i missing something?
And i've tested with more than one listview control...
Edit: I'm using it like this:
Private Sub UserListResize()
If Me.UserList.InvokeRequired Then
Me.UserList.Invoke(Sub() UserListResize())
Else
UserList.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
End If
End Sub
I'm calling this when i'm finished with adding items on the listview.
They dont do the exactly the same thing, but it is sometimes hard to tell them apart.
HeaderSize resizes each column to the greater of HeaderText.Length or longest cell content, thereby never clipping the header text.
ColumnContent resizes to longest cell content which will clip header text if the HeaderText is longer then the content. Test this with a column containing 1 or 2 digits/characters, but a long header like "FooBar Counter of Foo".
Neither mode will clip column text though, which means when you are economic with header text, it can look like they do the same thing. Other things to consider:
The LV must have items in it for ColumnContent to be meaningful
Inspite of Auto in the name, it is not a persistent setting - as soon as you add something else the layout may be incorrect, so you may have to reset it periodically.
You can always lay them out how you want and set AllowColumnResize to False
You set AutoResizeColumn on a per column basis (note it is singular)
Individual column widths can be "auto sized" using the Width Property and the Magic Numbers of -1 (set to content) or -2 (size to Header). This allows you to only apply the AutoResize logic to only some columns or use a different setting depending on the column:
For n As Integer = 0 To myLV.Columns.Count -1 Step 2 ' just do every other
myLV.Columns.Width = -2 ' -2 = size to column header text
' -1 = size to longest content
Next n
One place this is very handy is when adding columns dynamically. Rather than guessing at the TextExtent, you can set the new column's width to something at least somewhat applicable to start with, then change it the first time content is added. Reference: MSDN ColumnHeader Width Property.
Related
I have a datagridview whose styling is kept as follows
dgvCreatinine.AutoGenerateColumns = False
dgvCreatinine.AutoSize = False
dgvCreatinine.AutoResizeRows()
dgvCreatinine.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill
dgvCreatinine.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders)
dgvCreatinine.DataSource = dtTemp
It is appearing as shown in the attached image
As you can see the gridview is not resizing to the content along row wise. Could someone tell me how this can be accomplished? Thanks in advance.
You can just set the height of the entire control using this formula
DataGridViewHeight = HeaderHeight + NumberOfRows * RowHeight
Code:
dgvCreatinine.Height =
dgvCreatinine.ColumnHeadersHeight _
+ dgvCreatinine.Rows.OfType(Of DataGridViewRow).First().Height * dgvCreatinine.Rows.Count()
This works, but could use some tweaking. For example, there may be a better way to get the row height. And you may also need to add a pixel or two depending on borders. Also, if you have an underlying DataSource, you could get the rows count from it.
That code should be called whenever a row is added or removed, or DataBindings are updated etc.
I try to implement the following in winform vb net project (I see this work in an app written in delphi).
I wish to hide or set visibility to false of the bottom row that partially visible in dgv that is docked to fill.
I tried to implement something like this:
DataGridView1.Rows(DataGridView1.DisplayedRowCount(true) - 1).Visible = False
I think it should be called during DataBindingComplete and Resize/scroll events, but it doesn't work.
Do you have any ideas / solutions?
So what I use on one of my datagridviews is:
Dim ind As Integer = 0
ind = DataGridView1.Rows.Count - 1
DataGridView1.Rows(ind).Visible = False
which hides the last displayed row of the datagridview.
You requirement sounds somewhat odd. Your comment ”I wish to hide or set visibility to false of the bottom row that partially visible in dgv that is docked to fill.”... I am curious how you would know this is the last row? Is it not possible that there are more rows below the last one visible? If the scroll bars are available you should see the vertical one if rows go beyond its bounding box. If one of the rows is cut in half by the bounding box and there is more than 1 row below this row, then making invisible/hiding/deleting that row will simply move the next one up.
Since the DataGridView is docked you may have to resize the rows manually if you do not want the rows to be split by the bounding box. Another possible solution is to use the DataGridViews AutoSizeRowsMode Like below.
dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells;
This will set the rows so that a row will not be chopped if it is outside the bottom of the bounding box. The rows will auto size to fit evenly There are seven (7) AutoSizeRowsMode options and I am guessing one of them may do what you are looking for. I am guessing DisplayedCells may work for what you describe. If the grid is re-sized often, you may have to implement this row resizing. Hope this helps.
Is it possible to have a report with flexible text width and height? I sometimes have two words in this text and some times hundreds. I want to have small text for the first and big text for the second. How do I do that?
I would advise you to set a your text box size to what you think is optimum and use the CanShrink and CanGrow properties (tap the text box and then open the properties windows and you can find them there).
The CanGrow property indicates whether the size of the text box can increase vertically according to its content. Similarly, CanShrink decreases the height of the text box according to its content. Here is a link for better understanding these two properties.
Use the Detail_Format event.
It fires before each line, and you can change the formatting based on the length of the text.
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
If Len(Field1) < 10 Then
txtField1.FontSize = 18
Else
txtField1.FontSize = 12
End If
End Sub
Is there a way to automatically add a new line or something when a column in my ultraGrid is full? Sometimes when i load some text into a column in my ultragrid it has a longer value length than the available column field, as a result some text stays hidden. So i want when this happens to show the rest of the text in another line. Is this possible in vb.net for ultrawingrid?
You could set the CellMultiLine property as in the example below. (supposing you have one band and the column with the long text is the second one)
UltraGrid1.DisplayLayout.Bands(0).Columns(1).CellMultiLine = DefaultableBoolean.True
UltraGrid1.DisplayLayout.Bands(0).Columns(1).VertScrollBar = True
UltraGrid1.DisplayLayout.Override.DefaultRowHeight = 100
The next two lines are optional, but they serve the purpose to show immediately the column with a different height and with a vertical scrollbar
I have a strange problem and it's probably a simple fix, but after much research, I cannot seem to find a solution.
I have a DataGridView on which I'm trying to center the column headings, but the result is a left bias in the centering—almost like an indenting problem. I've seen a few posts on this issue on a site or two, but never a solution. Any thoughts?
Here's the statement I'm currently trying to use:
DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
The code you've posted is on the right track: you need to set the ColumnHeadersDefaultCellStyle property of your DataGridView control.
However, you need to create a new DataGridViewCellStyle class and assign that to the ColumnHeadersDefaultCellStyle property. You can't modify the Alignment property as your code sample shows unless you have assigned a DataGridViewCellStyle class to this property.
So, for example, the following code achieves perfectly centered column headings in a blank project:
Dim dgvColumnHeaderStyle As New DataGridViewCellStyle()
dgvColumnHeaderStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
myDataGridView.ColumnHeadersDefaultCellStyle = dgvColumnHeaderStyle
In the future, you may find it easier to do these types of things from the Designer. If you still need to do it yourself through code, you can check the *.Designer.vb file that is created to see how it was done.
EDIT: I just now noticed the slight offset you're referring to in the columns—it does indeed create a little extra padding to the right of each header. It's not a bug, though. There's a much simpler explanation.
Like a ListView, the DataGridView supports sorting by columns. Therefore, each column header reserves enough space to display the sort glyph (usually an arrow) when calculating center justification.
If you want the column headers to be perfectly centered, you'll need to disable sorting. Set the SortMode property for the column to "NonSortable". This should prevent space from being reserved for the sort glyph whenever the column text is center or right justified.
If you want to center or use any other alignment style of the Column Header text you can use this
dgvResults.Columns("ColumnName").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter