Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to change the button text on a bound datagrid view. I created my text columns and button columns via code and then I bound my dataset to the datagridview. Here is an example of how I created my button column:


VB
Public Sub CreateBoundButtonColumn(ByVal DGV As DataGridView, ByVal ColumnName As String, ByVal ColumnWidth As Integer, ByVal ColumnNum As Integer)

        'This sub adds an unbound column to the DGV

        ' Initialize the button column.
        Dim buttonColumn As New DataGridViewButtonColumn

        Dim newPadding As New Padding(1, 1, 1, 1)

        With buttonColumn
            .HeaderText = ColumnName
            .Name = ColumnName
            .Text = ColumnName
            .DefaultCellStyle.BackColor = Color.LightGray
            .DefaultCellStyle.Padding = newPadding
            .Width = ColumnWidth
            '.UseColumnTextForButtonValue = True
        End With

        'Add the button column to the control
        DGV.Columns.Insert(ColumnNum, buttonColumn)

    End Sub


If I set usecolumntextforbuttonvalue = true then the each button in the grid has the same text as the column name. I want each row to have different button text so I left it out. After I did that, now there is no text in the button.

I found a post here on Code Project related to this issue but I have some questions regarding it. Since it was pretty old I decided to open a new thread.

For reference, here is a link to the other article:

Change Button text in Datagridview in vb.net window application (Binding Time)[^]

The other article mentions that I should go to the databound event and find the button and then set the text of the button. If I want different text on each button cell, does this mean that I have to loop through all the rows in the datagridview and change them that way? I was able to get that to work but I thought there should be a better way? Is this my only alternative or can I so something before databinding which would eliminate the looping.

Thanks,
Posted
Comments
Sergey Alexandrovich Kryukov 15-May-13 12:25pm    
Try to assign string value to DataGridViewCell.Value.
—SA

1 solution

You say that you want to change the displayed text of each button individually, but you have given no indication of what the source source of this text will be. What is the source of this text?

Why do you call the method "CreateBoundButtonColumn" (Bound) when the comments and the code indicate that it is Unbound?

If it is unbound, then I would suggest using the RowPrePaintEvent to set the cell value
VB
Private Sub DataGridView1_RowPrePaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DataGridView1.RowPrePaint
   DataGridView1.Rows(e.RowIndex).Cells(IndexButtonColumn).Value = SomeStringValue
End Sub

You mention looping through the rows. This indicates to me that the text that you want to assign to the button is related to other values in the row. If it is, then build the "SomeStringValue" as needed.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900