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

时间:2013-08-17 21:20:52

标签: vb.net text datagridview cell

我有一个DataGridViewTextBoxColumn,它包含不同类型的DataGridViewCells(Combobox,Text& Button)。

Different DataGridView Cells

以下是我构建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

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

我正在尝试做什么?

1 个答案:

答案 0 :(得分:3)

经过进一步调查后我发现了我的问题......当我尝试使用CellBtn.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

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