Winform Datagridview button column button with multiline text

260 Views Asked by At

I've created a button column successfully based on the following post, including adding text to the individual buttons:

How to add a button to a column in the DataGridView

The problem is, my text won't fit on a single line, so I'm hoping to create a multiline button label.

Here is the code I'm using to define the column, but the Environment.Newline doesn't seem to work on these buttons. Is there a way to create multiline button label for datagridview buttons and, if so, how?

        Dim inspectionNotesButtonCol As New DataGridViewButtonColumn With {
            .Name = "InspNotesButton",
            .HeaderText = "Insp." + Environment.NewLine + "Notes",
            .Text = "Insp." + Environment.NewLine + "Notes",
            .UseColumnTextForButtonValue = True,
            .Width = 50
        }
        Me.dgvIssues.Columns.Add(inspectionNotesButtonCol)

Answers in either C# or vb.net are appreciated.

2

There are 2 best solutions below

2
Lajos Arpad On

You will need to set the MultiLine property to True and then try to use Environment.NewLine. There are several controls sharing the behavior you have experienced, TextBox is another example.

0
JohnG On

Setting the grids DefaultCellStyle.WrapMode and AutoSizeRowsMode should allow you to display multiple lines in the button cells. Something like…

Dim inspectionNotesButtonCol As New DataGridViewButtonColumn With {
        .Name = "InspNotesButton",
        .HeaderText = "Insp." + Environment.NewLine + "Notes",
        .Text = "Insp." + Environment.NewLine + "Notes",
        .UseColumnTextForButtonValue = True,
        .Width = 50
    }
dgvIssues.Columns.Add(inspectionNotesButtonCol)
dgvIssues.DefaultCellStyle.WrapMode = DataGridViewTriState.True
dgvIssues.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
dgvIssues.Rows.Add()