mond007
Active member
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 :
It is the AnswerSectionColor columns I am trying to create as a button.
I have the following code :
I have tried numerous ways : Like the following but all involve creating a column and NOT using an existing column from my datasource.
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
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
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 am unable to map the existing column to a button.
Would appreciate some help.
Thanks