1

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.

E. A. Bagby
  • 824
  • 9
  • 24

2 Answers2

1

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()
JohnG
  • 9,259
  • 2
  • 20
  • 29
0

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.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • There is no Multiline option for DatagridViewButtonCell or DataGridViewButtonColumn. What object would I set Multiline to? – E. A. Bagby Nov 01 '21 at 21:04
  • @EdwardBagby you are right and I was wrong. So, let's try with \n instead of Environment.NewLine. If it works, I will edit my answer. If not, I will remove it. – Lajos Arpad Nov 01 '21 at 21:10