C# DataGridViewButtonCell set buttons text
Asked Answered
B

10

19

I need to add my DataGridViewButtonCell to Column, and I need to name each other with different names.

But I didn't find any text properties.

Can anyone help me, please?

i do that stuff

DataGridViewButtonCell b = new DataGridViewButtonCell(); 
b.Value = "name"; 
MainTable.Rows.Add(b);

and it doesn't work

Ballistics answered 3/10, 2013 at 12:56 Comment(1)
If you set the DataGridViewCell.Value property as most answers suggest it will change the actual Value. Often not acceptable. Use the CellFormatting event and set the e.Value there!Elnaelnar
D
7

I admit that I don't understand why your method wouldn't work, but I was able to get the following to do what you wanted as a work-around:

        DataGridViewButtonCell b = new DataGridViewButtonCell();
        int rowIndex = MainTable.Rows.Add(b);
        MainTable.Rows[rowIndex].Cells[0].Value = "name";

In my example I'm assuming you only have one column (a DataGridViewButtonColumn). You should be able to modify this however you like so long as you set the value after you add the row. Again, not sure why that's the case...

Drollery answered 19/11, 2013 at 22:55 Comment(0)
B
51

If you want all the buttons to have same text use UseColumnTextForButtonValue property.

If you want different text for each button then use DataGridViewCell.Value property

Brownout answered 3/10, 2013 at 13:11 Comment(2)
UseColumnTextForButtonValue was exactly what I wanted. Set it true and set the column's Text property to what you want displayed.Ostyak
When UseColumnTextForButtonValue is false and the gridview VirtualMode is true then the column needs to be databound for the text to display, unless you subclass the buttoncell and override the Value(GetValue/SetValue) then set it as the cell template on the column.Woodbury
F
21

If you want to set the same text for all buttons, you can:

  1. In GridView properties click on edit columns
  2. Click on your button column
  3. Set Text to the text you want to see on each button
  4. Set UserColumnTextForButtonValue to True

See below:

Edit Button Column

Firebreak answered 6/11, 2017 at 9:37 Comment(0)
D
7

I admit that I don't understand why your method wouldn't work, but I was able to get the following to do what you wanted as a work-around:

        DataGridViewButtonCell b = new DataGridViewButtonCell();
        int rowIndex = MainTable.Rows.Add(b);
        MainTable.Rows[rowIndex].Cells[0].Value = "name";

In my example I'm assuming you only have one column (a DataGridViewButtonColumn). You should be able to modify this however you like so long as you set the value after you add the row. Again, not sure why that's the case...

Drollery answered 19/11, 2013 at 22:55 Comment(0)
L
7

I know you got your answer accepted but I added these in constructor and it worked for me. The only thing I was missing is UseColumnTextForButtonValue = true

public Form1() 
    {
        InitializeComponent();
        var editColumn = new DataGridViewButtonColumn
        {
            Text = "Edit",
            UseColumnTextForButtonValue = true,
            Name = "Edit",
            DataPropertyName = "Edit"
        };
        dataGridView1.Columns.Add(editColumn);
    }
Leia answered 30/7, 2015 at 7:34 Comment(1)
that's nice answer, I solved it long time ago, but that's the best if it really works, can't check it for myself, but anyone looking for an answer better try thisBallistics
I
5

Use this to show the same text on all buttons with default button at first

DataGridView.Columns["Buttons_Index"].DefaultCellStyle.NullValue = "name";
Indoor answered 8/5, 2015 at 4:54 Comment(0)
H
0

Try

yourDataGridView.Rows[rowIndex].Cells[columnIndex].Value
Hertfordshire answered 3/10, 2013 at 13:4 Comment(3)
i do that stuff DataGridViewButtonCell b = new DataGridViewButtonCell(); b.Value = "name"; MainTable.Rows.Add(b); and it doesn't workBallistics
just button column, thats allBallistics
You also need add DataGridViewButtonColumn to table DataGridViewButtonColumn column = new DataGridViewButtonColumn (); column.Name = "Buttons"; MainTable.Columns.Add(column);Hertfordshire
S
0

you looking for DataGridViewCell.Value Property

Scalage answered 3/10, 2013 at 13:7 Comment(1)
i do that stuff DataGridViewButtonCell b = new DataGridViewButtonCell(); b.Value = "name"; MainTable.Rows.Add(b); and it doesn't workBallistics
A
0

If you have just one column and that's a buttoncolumn by default

MainTable.Rows.Add("Some text");

will suffice.

Acariasis answered 26/3, 2014 at 9:4 Comment(2)
hopes it will help smbdy who use just one column.Ballistics
yeah actually. one who uses just one button column for a whole dgv might need a lot of help 8)Acariasis
I
-1

[Colimn.Name].Text = "Click Me"

[Colimn.Name].UseColumnTextForButtonValue = True

button_Text Use this code in Form_Load event-it will name all buttons in that column as "Click Me".

Invertase answered 19/4, 2021 at 4:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.