更改绑定的datagridview中的按钮文本 [英] Change button text in bound datagridview

查看:94
本文介绍了更改绑定的datagridview中的按钮文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改绑定数据网格视图上的按钮文本。我通过代码创建了文本列和按钮列,然后将数据集绑定到datagridview。以下是我创建按钮列的示例:





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:


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





如果我设置usecolumntextforbuttonvalue = true,则网格中的每个按钮都与列名称具有相同的文本。我希望每一行都有不同的按钮文字,所以我把它留了出来。在我这样做之后,现在按钮中没有文字。



我在Code Project上找到了与此问题相关的帖子,但我对此有一些疑问。由于它很老,我决定开一个新线程。



作为参考,这里是另一篇文章的链接:



更改按钮文本在VB中.net窗口应用程序(绑定时间) [ ^ ]



另一篇文章提到我应该去数据绑定事件找到按钮然后设置按钮的文本。如果我想在每个按钮单元格上使用不同的文本,这是否意味着我必须循环遍历datagridview中的所有行并以这种方式更改它们?我能够让它工作,但我认为应该有更好的方法?这是我唯一的选择还是我可以在数据绑定之前做些什么来消除循环。



谢谢,



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,

推荐答案

你说你想单独更改每个按钮的显示文字,但是你没有说明这个文本的来源是什么。这篇文章的来源是什么?



当评论和代码表明它是Unbound时,为什么要调用方法CreateBoundButtonColumn(Bound)? />


如果它是未绑定的,那么我建议使用RowPrePaintEvent设置单元格值

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
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



你提到循环遍历行。这向我指出,您要分配给按钮的文本与行中的其他值相关。如果是,则根据需要构建SomeStringValue。


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.


这篇关于更改绑定的datagridview中的按钮文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆