Create Button in DataGridView using an Existing Column from BindingSource1.DataSource

mond007

Active member
Joined
Apr 26, 2010
Messages
37
Location
near Solihull, Birmingham UK
Programming Experience
10+
Hi,

I am trying to create a button in a DatagridView using an existing column. I have found plenty of examples where a column is added at the end or in between columns but not any that actually use the column from the binding source. (As I plan to Save a display the color from Integer stored value)

My columns are :

VB.NET:
        'QuestionID
        'QuestionNo
        'Question
        'AnswerSection
        'AnswerSectionColor ---- (integer).
        'AnswerImage
        'AnswerHyperlink
It is the AnswerSectionColor columns I am trying to create as a button.
DatagridViewButton.jpg

I have the following code :
VB.NET:
Public Class FormCustomQuestionsAdministration
    Public Property DetectUrls As Boolean
    Public Property SelectionIndent As Integer
    Public Property SelectionHangingIndent As Integer
    Dim QuestionAnswerData As New DataSet
    Private Sub FormCustomQuestionsAdministration_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        '--------------------------------------------------------------------------
        Dim bsQuestions As New BindingSource
        QuestionAnswerData.ReadXml(GlobalVariables.RootPath.ToString & GlobalVariables.RootXMLFileName.ToString, XmlReadMode.ReadSchema)
        With QuestionAnswerData.Tables(0).Columns(0)
            .AutoIncrement = True
            .AutoIncrementStep = 1
            If QuestionAnswerData.Tables(0).Rows.Count > 0 Then
                .AutoIncrementSeed = QuestionAnswerData.Tables(0).Rows.Cast(Of DataRow).Max(Function(x) CInt(x(0))) + 1
            Else
                .AutoIncrementSeed = 1
            End If
        End With
        With bsQuestions
            .DataSource = QuestionAnswerData
            .DataMember = "Questions"
        End With
        Me.BindingSource1.DataSource = QuestionAnswerData                 'Bind the parent source to the parent table.
        Me.BindingSource1.DataMember = "Questions"
        Me.DataGridView1.DataSource = Me.BindingSource1
        With DataGridView1
            .ColumnHeadersVisible = False
            .AllowUserToAddRows = False
            .AllowUserToResizeColumns = False
            .SelectionMode = DataGridViewSelectionMode.FullRowSelect
            .MultiSelect = False
            .Columns(0).Visible = False
            .RowHeadersVisible = False
            ?.etc
        End With
        Dim bc As New DataGridViewButtonColumn
        bc.Tag = False
        bc.Text = "Delete"
        bc.Name = "Delete"
        bc.Width = 19
        DataGridView1.Columns.Add(bc)
        ActiveControl = DataGridView1
        DataGridView1.ReadOnly = False   ' Disable entire DataGridView to Read Only then set all the columns in your code as readonly.
        DataGridView1.Columns("QuestionNo").ReadOnly = True                 ' Disable changing of Main Question ID to prevent mismatch problems.
    End Sub

I have tried numerous ways : Like the following but all involve creating a column and NOT using an existing column from my datasource.

VB.NET:
        Dim buttonColumn As New DataGridViewButtonColumn()  '        ' Add a button column.  
        buttonColumn.HeaderText = ""
        buttonColumn.Name = "AnswerSectionColor"
        buttonColumn.Text = "Colour"
        buttonColumn.UseColumnTextForButtonValue = True
I have also tried cellpainting but this only allows a button image into the cell/column. I quote like the Standard Button with text as I plan to change the backcolor of the button to the one selected from the color picker.
I am unable to map the existing column to a button.

Would appreciate some help.

Thanks
 
To link the grid column with the data source field you have to set the columns DataPropertyName property.
Also when you configure bindings in designer and get grid columns generated there you can change column type for an existing bound column.
When visual styles are enabled, the buttons in a button column are painted using a ButtonRenderer, and cell styles specified through properties such as DefaultCellStyle have no effect.
Changing appearance of DataGridViewButtonCell requires CellPainting handling, or writing an inherited class and override Paint, which can be lots of complicated work.
It can be done simpler by changing the columns FlatStyle to Flat/Popup, and change each cells .Style.BackColor for example using CellFormatting event. Button will look more like Windows 8 buttons, and pretty much how a standard form button will look when you change its BackColor.
 
Ok cool.

I am not sure of the code, but I will research and apply the following :
It can be done simpler by changing the columns FlatStyle to Flat/Popup, and change each cells .Style.BackColor for example using CellFormatting event. Button will look more like Windows 8 buttons, and pretty much how a standard form button will look when you change its BackColor.

Thanks Kuldip
 

Latest posts

Back
Top