Click here to Skip to main content
15,917,061 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear experts,
each time I collect data from database, I have more than 500 records and each data need to be digested before I populate in customised datagridview. By using "For Each" looping, it took 10 - 15 seconds, so I decided to use datasource to add the data into datagridview.

But how can I make some of the existing columns become DataGridViewButtonColumn if I am using datasource to add data into datagridview?
Posted

1 solution

Manually define you column at runtime.


VB
Datagridview1.DataSource = MyDatatable

With Datagridview1
 .Clear
 .AutoGenerateColumns = False

'Textbox Column
 .Columns.Add("ColumnName1", "Column Name 1")
 .Columns(0).DataPropertyName = "FileName"'Datatable column Name
 .Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft

'Checkbox column
 Dim m_Active As New DataGridViewCheckBoxColumn
 m_Active.HeaderText = "Active"
 m_Active.DataPropertyName = "Active"
 .Columns.Add(m_Active)
 .Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft

'Button Column
Dim m_ButtonColumn As New DataGridViewButtonColumn
m_ButtonColumn.HeaderText = "SomeValue"
m_ButtonColumn.DataPropertyName = "SomeValue"
.Columns.Add(m_ButtonColumn)

End With
 
Share this answer
 
v2
Comments
Teoh Chia Wei 25-Jul-13 23:58pm    
Winston Madiano,
For the Button Column, is this method adding a new column or changing the existing column to button column?
Winston Madiano 26-Jul-13 0:13am    
Since we set AutoGenerateColumns = False the Datagridview1 has no columns, we have to manually add them and set the properties on how we want to display the data.
This will add a column with header text(SomeValue) and the cell values would be buttons.
Teoh Chia Wei 26-Jul-13 2:17am    
I have customized the datagridview with textboxcolumn and buttoncolumn.... but when I apply the datatable to the datasource... I got blank record

dgvList.AutoGenerateColumns= False
dgvList.Datasource = ds.Tables(0)
Winston Madiano 26-Jul-13 5:03am    
make sure that the DataPropertyName has the same column name on your datatable column.
vtpindia 9-Dec-14 5:59am    
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900