vb.net 如何在具有多个控件的列中设置 DataGridViewButtonCell 文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18293458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:42:43  来源:igfitidea点击:

How to set DataGridViewButtonCell Text In A Column with Multiple Controls

vb.nettextdatagridviewcell

提问by Alex

I have a DataGridViewTextBoxColumn that holds different types of DataGridViewCells (Combobox, Text & Button).

我有一个 DataGridViewTextBoxColumn,它包含不同类型的 DataGridViewCells(组合框、文本和按钮)。

Different DataGridView Cells

不同的 DataGridView 单元格

Here is how I build the datagridview rows:

这是我构建 datagridview 行的方法:

Public Shared Function BuildDgvRow(ByVal tq As clsTabsQuestion) As DataGridViewRow
    Dim Row As New DataGridViewRow
    Dim ComboBoxCell As New DataGridViewComboBoxCell
    Dim CellBtn As New DataGridViewButtonCell //File Upload
    Dim CellTxtQ As New DataGridViewTextBoxCell //Cell question
    Dim CellTxt As New DataGridViewTextBoxCell //Cell txt

    CellTxtQ = New DataGridViewTextBoxCell

    //Build row
    With Row
        //Add cell that will contain question
        .Cells.Add(CellTxtQ)

        //Add CheckBox / Button / Text to the other cell that will contain the answer
        Select Case tq.sType
            Case "YesNo"
                ComboBoxCell = New DataGridViewComboBoxCell()
                ComboBoxCell.Items.AddRange(New String() {"Yes", "No", "N/A"})
                .Cells.Add(ComboBoxCell)
            Case "FileUpload"
                CellBtn = New DataGridViewButtonCell
                .Cells.Add(CellBtn)
            Case "Text"
                CellTxt = New DataGridViewTextBoxCell
                .Cells.Add(CellTxt)
        End Select

        //Set row values
        .SetValues({tq.Title, ""})
        .Tag = tq
    End With

    Return Row
End Function

I cannot seem to get any "Text" property from the DataGridViewButtonCell class. Is there a way to set text on a DataGridViewButtonCell? This is a questionnaire and users are able to create their own. Therefore they have a choice to choose a combobox, text or button as an answer to their question.

我似乎无法从 DataGridViewButtonCell 类中获得任何“文本”属性。有没有办法在 DataGridViewButtonCell 上设置文本?这是一个问卷,用户可以创建自己的问卷。因此,他们可以选择组合框、文本或按钮作为他们问题的答案。

Is what I'm trying to do possible?

我正在尝试做的可能吗?

回答by Alex

After further investigation I figured out my problem ... When I tried using the CellBtn.ValueI forgot that I was resetting the row value to an empty string. Silly me.

经过进一步调查,我发现了我的问题......当我尝试使用时,CellBtn.Value我忘记了我正在将行值重置为空字符串。傻我。

Here's how I solved it (two ways)

这是我解决它的方法(两种方式)

Setting Text using DataGridViewButtonCell.Value

使用 DataGridViewButtonCell.Value 设置文本

Public Shared Function BuildDgvRow(ByVal tq As clsTabsQuestion) As DataGridViewRow
    Dim Row As New DataGridViewRow
    Dim ComboBoxCell As New DataGridViewComboBoxCell
    Dim CellBtn As New DataGridViewButtonCell //File Upload
    Dim CellTxtQ As New DataGridViewTextBoxCell //Cell question
    Dim CellTxt As New DataGridViewTextBoxCell //Cell txt

    CellTxtQ = New DataGridViewTextBoxCell

    //Build row
    With Row
        //Add cell that will contain question
        .Cells.Add(CellTxtQ)

        //Add CheckBox / Button / Text to the other cell that will contain the answer
        Select Case tq.sType
            Case "YesNo"
                ComboBoxCell = New DataGridViewComboBoxCell()
                ComboBoxCell.Items.AddRange(New String() {"Yes", "No", "N/A"})
                .Cells.Add(ComboBoxCell)
            Case "FileUpload"
                CellBtn = New DataGridViewButtonCell
                CellBtn.Value = "ASdasdwd"
                .SetValues({tq.Title, CellBtn.Value})
                .Cells.Add(CellBtn)
            Case "Text"
                CellTxt = New DataGridViewTextBoxCell
                .Cells.Add(CellTxt)
        End Select

        //Set values
        If tq.sType <> "FileUpload" Then .SetValues({tq.Title, ""})
        .Tag = tq
    End With

    Return Row
End Function

Setting Text using DataGridViewRow.SetValue

使用 DataGridViewRow.SetValue 设置文本

Public Shared Function BuildDgvRow(ByVal tq As clsTabsQuestion) As DataGridViewRow
    Dim Row As New DataGridViewRow
    Dim ComboBoxCell As New DataGridViewComboBoxCell
    Dim CellBtn As New DataGridViewButtonCell //File Upload
    Dim CellTxtQ As New DataGridViewTextBoxCell //Cell question
    Dim CellTxt As New DataGridViewTextBoxCell //Cell txt

    CellTxtQ = New DataGridViewTextBoxCell

    //Build row
    With Row
        //Add cell that will contain question
        .Cells.Add(CellTxtQ)

        //Add CheckBox / Button / Text to the other cell that will contain the answer
        Select Case tq.sType
            Case "YesNo"
                ComboBoxCell = New DataGridViewComboBoxCell()
                ComboBoxCell.Items.AddRange(New String() {"Yes", "No", "N/A"})
                .Cells.Add(ComboBoxCell)
            Case "FileUpload"
                CellBtn = New DataGridViewButtonCell
                .Cells.Add(CellBtn)
            Case "Text"
                CellTxt = New DataGridViewTextBoxCell
                .Cells.Add(CellTxt)
        End Select

        //Set values
        If tq.sType = "FileUpload" Then .SetValues({tq.Title, "Upload"}) Else .SetValues({tq.Title, ""})
        .Tag = tq
    End With

    Return Row
End Function