Change DataGridView Column Type

  • Thread starter Thread starter Peter
  • Start date Start date
P

Peter

How to I changed bound column in DataGridView from DataGridViewTextBoxColumn(); to DataGridViewButtonColumn() ?

Thanks


Peter
 
Hi Peter,

To use a DataGridViewButtonColumn, you just need to create such an object,
set its DataPropertyName property to the column name in the DataTable, and
then add the column to the DataGridView column collection. You can check
the following links for more information:

http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridviewbu
ttoncolumn.aspx

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Kevin Yu said:
Hi Peter,

To use a DataGridViewButtonColumn, you just need to create such an object,
set its DataPropertyName property to the column name in the DataTable, and
then add the column to the DataGridView column collection. You can check
the following links for more information:

http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridviewbu
ttoncolumn.aspx

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."


Thank you for you help!

The example shows me how to add a button column, but I don't want to add
extra column I want to convert existing column to a button after I bind it
with the dataset. How do I do that?
 
Hi Peter,

With the code I provided in my last post, we can add a column with buttons
to the DataGridView. Then we can check in the DataGridView.Columns to
remove the original column with textboxes.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Dear Kevin,

I did what you say but my new column has no values in its cells. It says is
databounded and the datapropertyname is right, but all cells are empty with a
value of Nothing...

I have:
myDGV.DataSource = my DataTable
'Since I want to change an existing column I call a sub as
ChangeToButton("MyColumn")

The ChangeToButton Sub is:
Private Sub ChangeToButtonColumn(ByVal strColumnName As String)
Dim buttons As New DataGridViewButtonColumn()
With buttons
.Name = strColumnName
.HeaderText = strColumnName
.DataPropertyName = strColumnName
'strColumnName is "MyColumn", which is a fields in MyTable the datasource
for MyDGV
.UseColumnTextForButtonValue = True
.FlatStyle = FlatStyle.Standard
.DisplayIndex = 6
End With
' I get rid of the original column
MyDGV.Columns.Remove(strColumnName)
'I add the new column
MyDGV.Columns.Add(buttons)
End Sub

Can you tell me what I am doing wrong?

Thank you

--
Sergio Torres C.
(505) 897 2041
___________________
http://www.stcsys.com
___________________
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top