Kai
Kai

Reputation: 2168

DataGridView button text not appearing despite UseColumnTextForButtonText set to true

I've added a button column to a DataGridView and want to display the text "Compare" on it. I've set the Text property to Compare and UseColumnTextForButtonValue to True, but no text displays:

Properties set, no text visible

This is also true at runtime, so it's not just not displaying in the designer:

No text at runtime either

How do I get the text to appear?

Edit: For prosperity's sake, here's the code in the generated Designer.cs file. I haven't added any code to this form myself yet, so there's no chance that something's resetting it further down the line.

// 
// Compare
// 
dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
dataGridViewCellStyle1.Font = new System.Drawing.Font("Calibri", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Compare.DefaultCellStyle = dataGridViewCellStyle1;
this.Compare.HeaderText = "Compare";
this.Compare.Name = "Compare";
this.Compare.Text = "Compare";
this.Compare.ToolTipText = "Compare the dictionary definition to the system definition";
this.Compare.UseColumnTextForButtonValue = true;

Upvotes: 9

Views: 13749

Answers (3)

Joern
Joern

Reputation: 1

While having the same problem, the provided answers either did not fit my requirements (adding more rows while the last row still does not show the button-text) or have not worked full functioning (row index in the CellValueChanged event needs to be >= 0). I also wanted to fit this behavior with the AllowUserToAddRows property functionality.
To do so, the RowsAdded event of the datagridview needs some adjustments as well.
Here is what worked for me:
UseColumnTextForButtonValue set to true (for each DataGridViewButtonColumn) and set its Text.

private void dataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
  var grid = (DataGridView)sender;
  if (grid.Columns[e.ColumnIndex] is DataGridViewButtonColumn)
  {
    if (grid.RowCount >= 0)
    {
       //this needs to be altered for every DataGridViewButtonColumn with different Text
       grid.Rows[grid.RowCount - 1].Cells[e.ColumnIndex].Value = "ButtonText";
    }
  }
}

private void dataGridView_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
  var grid = (DataGridView)sender;
  //this needs to be altered for every DataGridViewButtonColumn with different Text
  int buttonColumn = 3;
  if (grid.Columns[buttonColumn] is DataGridViewButtonColumn)
  {
    if (grid.RowCount >= 0)
    {
        grid.Rows[grid.RowCount - 1].Cells[buttonColumn].Value = "ButtonText";
    }
  }
}

Upvotes: 0

刘海龙
刘海龙

Reputation: 11

Kif gets the right answer:

DataGridViewButtonColumn does not display the text in the button on the last row. Add some more rows, and the text will appear in all but the last row.

but if you wanna all shows the text you want(include the last row),you can simply realize the event handler CellValueChanged and set the cell value like this:

_yourDataGridView[Compare.Name, rowIndex].Value = "Compire";

Upvotes: 1

Kif
Kif

Reputation: 265

DataGridViewButtonColumn does not display the text in the button on the last row. Since in your example you only display one row, it is not shown. Add some more rows, and the text will appear in all but the last row.

Upvotes: 7

Related Questions