Vb.net – How to set DataGridViewButtonCell Text In A Column with Multiple Controls

celldatagridviewtext;vb.net

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

Different DataGridView Cells

Here is how I build the datagridview rows:

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.

Is what I'm trying to do possible?

Best Answer

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

Here's how I solved it (two ways)

Setting Text using 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

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