How to resize row height in C1FlexGrid? - vb.net

I need to auto resize row height in a C1FlexGrid. I need to make it work using AutoSizeRow but it does not change the row height. I tested it by setting height and it works. Why is AutoSizeRow not working?
For i As Integer = 0 To fgrid.Rows.Count - 1
'Setting the height explicitly changes the row height
fgrid.Rows(i).Height = 32
'But AutoSizeRow() does not change the row height
fgrid.AutoSizeRow(i)
Next i

Please note that the AutoSizeRow method works when there is any data filled in the grid's row. If there isn't any data, AutoSizeRow would simply wont work. The same thing is happening in you snippet. Since there is no data in the row, fgrid.AutoResize(i) is useless. Try replacing your snippet with the following, and you would understand how AutoSizeRow works:
For i As Integer = 0 To fgrid.Rows.Count - 1
'Fill data in the cell
fgrid.Rows(i)(1) = "This is sample text"
'Setting the height explicitly changes the row height
fgrid.Rows(i).Height = 32
'AutoSizeRow() is changing the row height now
fgrid.AutoSizeRow(i)
Next i

Related

How to make a table fit the window height in Word?

I am New to VBA. I have a table with 4 columns and almost 2000 rows and it is displayed in a 3 column Word layout. My problem is that the last rows of the 3 columns are not always on one height as shown in the image (Masked the personal data with the white font in cells). [Image of the Table1
I think a solution would be to set the height of the rows with 2-lines to 0.54cm and the 1-line rows to 0.27cm. I did this manually and it worked. I am looking for a macro to achieve this. The below code will provide an understanding of what I am trying to do.
Sub height ()
ActiveDocument.Tables(1).Rows.HeightRule = wdRowHeightAuto
'this automatically sets the 2-line rows to 0.53cm and the 1-line rows to 0.25cm
For Each row in Rows
If Row.RowHeight < 0.5cm Then
Row.RowHeight = 0.27cm
Else Row.RowHeight = 0.54cm
End If
Next Row
End Sub
I know that this code cant work but i think it shows you what i want to do.
As Row.RowHeight won't return a useful value unless the row has been set to an exact height you can change the line spacing for the text so that the required cell heights are achieved automatically.
The code below assumes that the table you want to adjust is the first one in the document.
Sub AdjustLineSpacingForTable()
Dim reqdLineSpacing As Single
'start with the height of a double height cell
reqdLineSpacing = CentimetersToPoints(0.54)
With ActiveDocument.Tables(1)
'table cells have padding top and bottom so we need to subtract that from the required height
reqdLineSpacing = reqdLineSpacing - (.TopPadding + .BottomPadding)
'now we can adjust the text spacing
With .Range.ParagraphFormat
'ensure there is no additional spacing on the paragraphs
.SpaceAfter = 0
.SpaceBefore = 0
'set line spacing to an exact amount
.LineSpacingRule = wdLineSpaceExactly
.LineSpacing = reqdLineSpacing / 2
End With
End With
End Sub

Some code working fine when executed step by step but malfunctioned when used in one go

I have used the Range.end property for achieving my target of setting the data into a table cell into particular number of lines.
For this first I calculate the instant cell height then keep on changing the cell width to achieve targeted cell height.
This property, when used in step by step execution, is giving the correct value, and I am able to achieve the targeted cell height. Range.end keeps on returning the same value even after the change in cell height, which must have changed the range.end value
'the table acted upon has 2 rows and one column and the column width as already 'set to some small value
With ActiveDocument.Tables(tableIndexTemp + 1)
'calculate cell height
posEnd = .Cell(1, 1).Range.End - 1
heightDaisRow = ActiveDocument.Range(posEnd, _ posEnd).Information(wdVerticalPositionRelativeToTextBoundary)
Do While heightDaisRow > minHeightDaisRow
'increase cell width
.Columns(1).Width = .Columns(1).Width + CentimetersToPoints(0.1)
'again calculate cell height
posEnd = .Cell(1, 1).Range.End - 1
heightDaisRow = ActiveDocument.Range(posEnd, _ posEnd).Information(wdVerticalPositionRelativeToTextBoundary)
Loop
End With
When executed step by step the value of posEnd changes with the change in width but when the same code is executed in one go the posEnd value does not change, which causes the cell width to keep on increasing and shows the error
Cell width can not be more than this particular value'.

How to set row height of a datagridview based on its contents in vb.net

I want to adjust row height to display entire contents of a cell(column width is fixed). I have one column and many rows in my datagridview.
I used following codes, but I couldn't get required row height
Me.data.DefaultCellStyle.WrapMode = DataGridViewTriState.True
data.AutoResizeRows(DtaGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders)
Set property AutoSizeColumnMode of datagridview to AllCells and check it.
DataGridView.AutoSizeRowsMode Property
try this
DataGridViewRow row = dataGridView.Rows[0];
row.Height = 15;
DataGridViewRow.Height Property
You can use this instead:
For i=0 to DataGridView1.Rows.Count-1
DataGridView1.Rows(i).Height=50
Next
It will consume less time and coding lines
To change the height of the rows you must to make a change to the specific row (On by On) like this
For i = 0 To DataGridView1.Rows.Count - 1
Dim r As DataGridViewRow = DataGridView1.Rows(i)
r.Height = 50
Next

DataGridView WrapMode + AutoSizeColumnsMode

How do I wrap my DataGridView's headers (long texts without break line) at the same time have it's AutoSizeColumnsMode to Fill?
For i As Integer = 0 To DataGridView1.Columns.Count - 1
DataGridView1.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
Next
I have this code to wrap the text, but will ignore my DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill (which leaves my DataGridView with empty gray spaces)
You are setting .AutoSizeMode property for every column in your DataGridView; to resolve your problem you can set .AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill on one column (i.e. the last visible column).
You must also set .ColumnHeadersDefaultCellStyle.WrapMode property to specify that a textual content in a DataGridView cell is wrapped to subsequent lines or truncated when it is too long to fit on a single line.
Code example:
YourDataGridView.ColumnHeadersDefaultCellStyle.WrapMode = False
For i As Integer = 0 To YourDataGridView.Columns.Count - 1
If i = YourDataGridView.Columns.Count - 2 Then
YourDataGridView.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
Else
YourDataGridView.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
End If
Next i
Please note that I use YourDataGridView.Columns.Count - 2 because my last column is not visible so I need to apply this property to the previous column.

Hide a gridview header column without losing position of other header columns vb.net

I would like to hide the header for two columns on a gridview. However I would like the other column headers to remain in their original position not move across to where the hidden columns would have been.
The code I'm using to hide the headers is the following:
For colCount As Integer = 0 To 1
gvProgressGrid.HeaderRow.Cells(colCount).Visible = False
Next
In the below example the headers should start at the column that contains the first set of blue cells.
Instead of toggling visibility, you can just set the column headers to empty strings and remove the border.
For colCount As Integer = 0 To 1
gvProgressGrid.HeaderRow.Cells(colCount).Text = ""
gvProgressGrid.HeaderRow.Cells(colCount).BorderStyle = BorderStyle.None
Next
If you want to retain the column header value, you could also just set the cell's foreground color equal to it's background color.
For colCount As Integer = 0 To 1
gvProgressGrid.HeaderRow.Cells(colCount).ForeColor = gvProgressGrid.HeaderRow.Cells(colCount).BackColor
gvProgressGrid.HeaderRow.Cells(colCount).BorderStyle = BorderStyle.None
Next