Removing columns from a datagridview

mike55

Contributor
Joined
Mar 26, 2004
Messages
727
Location
Ireland
I am binding a dataset to a datagridview. My dataset returns about 7 different columns, of which I only want 3 to be visible. To enable me to hide the additional columns, I have added the following code:
Code:
'Organisation Name
dCol.HeaderText = "Organisation Name"
dCol.Name = "OrganisationName"
dCol.DataPropertyName = "OrganisationName"
dCol.ReadOnly = True
dgvResults.Columns.Add(dCol)

'License Code
dCol = New DataGridViewTextBoxColumn
dCol.HeaderText = "License Code"
dCol.Name = "LicenseCode"
dCol.DataPropertyName = "LicenseCode"
dCol.ReadOnly = True
dgvResults.Columns.Add(dCol)

'Total Waste/Emissions
dCol = New DataGridViewTextBoxColumn
dCol.HeaderText = "Total Waste/Emissions"
dCol.Name = "Total"
dCol.DataPropertyName = "Total"
dCol.ReadOnly = True
dCol.Visible = false
dgvResults.Columns.Add(dCol)

I then have a refresh button that allows me to retrieve a new version of the data and automatically binds the data to the datagridview control. The problem is that some of the columns that I have specified as Visible=false are showing up.

Any suggestions on how I can check if the column already exists or simply delete the column in question?

Mike55.
 
Try trimming down your SELECT statement to only include those values that you want. you can also remove the "auto-generate columns" property; this will require you to manually add each column that you DO want, and all others will be ignored.
 
Need the extra columns as they contain timestamp and audit details. One of the columns that I bring back is binded to DataGridViewComboBoxColumn that contains three values. When the value is changed, I need to timestamp it and record what user made the changes. The user can then hit a save button that transports the updated dataset to the backend and calls the dataadapter.update method thus updating the database.
 
Back
Top