DataGridView WrapMode + AutoSizeColumnsMode - vb.net

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.

Related

CheckBox in DataGridView Won't Evaluate if Cell Has Focus

The first column in a DataGridView (dgv1) has checkboxes. Interestingly, when you click on the last checkbox desired, as soon a you loop through the rows for that column to evaluate checked items, the last cell checked (with a cursor on it) does not evaluate to true. The syntax for looping through the checkboxes in column 0 is below:
Dim NumCBChecked As Integer = 0
For i = 0 To dgv1.Rows.Count - 1
If CBool(dgv1(0, i).Value) = True Then
NumCBChecked += 1
End If
Next
Is there a way to set the focus to false if the cell containing the last checkbox checked is in edit mode?
BTW, refreshing the edit status (below) did not help before looping:
dgv1.RefreshEdit()
Per #JohnG's comment, the suggestion worked as follows:
dgv1.EndEdit()
Dim NumCBChecked As Integer = 0
For i = 0 To dgv1.Rows.Count - 1
If CBool(dgv1(0, i).Value) = True Then
NumCBChecked += 1
End If
Next

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

First 30 % displayed columns in DataGridView

First of all: 30% doesn't matter. That's a question of design. We can also say the first 3 Displayed Columns.
In my DataGridView I am using BackgroundColors for Rows to pass the User some information.
To keep this information visible to the user while Rows are being selected the first 30% of the columns should get the same SelectionBack/ForeColor as the Back/ForeColor.
So far that has never been a problem using
.cells(0).Style.SelectionBackColor = .cells(0).Style.Backcolor
(and so on).
Now I added the function that allows the user to reorder the Columns which makes the following Statement become true:
ColumnIndex != DisplayedIndex.
That statement beeing true makes the SelectionBackColor-Changed cells be somewhere mixed in the row and not in the first columns anymore. It still does the job, but looks terrible.
Is there something like a "DisplayedColumns" collection in order of the .DisplayedIndex Value that i could use to call the first few DisplayedColumns? If not, how could I effectivly create one my own?
Edit:
The user can also hide specific columns, that do not matter for him. So we have to be aware of Column.DisplayedIndex and Column.Visble
Got it working with the following code:
Try
' calculate what is thirty percent
Dim colcount As Integer = oDic_TabToGridview(TabPage).DisplayedColumnCount(False)
Dim thirtyPercent As Integer = ((colcount / 100) * 30)
' Recolor the first 30 % of the Columns
Dim i As Integer = 0
Dim lastCol As DataGridViewColumn = oDic_TabToGridview(TabPage).Columns.GetFirstColumn(DataGridViewElementStates.Visible)
While i < thirtyPercent
.Cells(lastCol.Index).Style.SelectionBackColor = oCol(row.Item("Color_ID") - 1)
.Cells(lastCol.Index).Style.SelectionForeColor = Color.Black
lastCol = oDic_TabToGridview(TabPage).Columns.GetNextColumn(lastCol, DataGridViewElementStates.Visible, DataGridViewElementStates.None)
i += 1
End While
Catch ex As Exception
MsgBox(ex.Message & vbNewLine & ex.StackTrace)
End Try
Let us first assume you are coloring your rows somehow resembling the following manner:
Me.dataGridView1.Rows(0).DefaultCellStyle.BackColor = Color.PowderBlue
Me.dataGridView1.Rows(1).DefaultCellStyle.BackColor = Color.Pink
' ...etc.
In the DataGridView.CellPainting event handler, you can determine if the painting cell falls within the first N columns by utilizing the DataGridViewColumnCollection.GetFirstColumn and DataGridViewColumnCollection.GetNextColumn methods.
If the cell belongs to one these columns, set the cell's SelectionBackColor to the cell's BackColor. Otherwise set it to the default highlighting color.
Dim column As DataGridViewColumn = Me.dataGridView1.Columns.GetFirstColumn(DataGridViewElementStates.Visible)
e.CellStyle.SelectionBackColor = Color.FromName("Highlight")
' Example: Loop for the first N displayed columns, where here N = 2.
While column.DisplayIndex < 2
If column.Index = e.ColumnIndex Then
e.CellStyle.SelectionBackColor = e.CellStyle.BackColor
Exit While
End If
column = Me.dataGridView1.Columns.GetNextColumn(column, DataGridViewElementStates.Visible, DataGridViewElementStates.None)
End While
As a side note: You may want to consider changing the ForeColor on these cells for readability - depending on your row's BackColor choices. Likewise, if only a single cell is selected from one these first N columns, it can be difficult to notice.

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

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