C# – Remove Column from DataGridView

cdatagridviewwinforms

I have a database that has a Users table and I present the data in a DataGridView. I would like to remove 4 columns, but the code I have (referenced from MSDN) seems to append the columns at the end. How can I totally REMOVE the columns?

So this is how the DGV looks without the columns removed

Without Removal Code

The Code I use to TRY and remove the columns

RadarServerEntities rse = new RadarServerEntities();
gvUsers.DataSource = rse.Users;

gvUsers.Columns.Remove("ID");
gvUsers.Columns.Remove("InsertDate");
gvUsers.Columns.Remove("Connections");
gvUsers.Columns.Remove("MachineID");

The result

With Removal Code

I would like to get rid of the last 4 columns, so why isnt my code doing it?

Many Thanks 🙂

Best Answer

I tend to just hide the fields instead.

gvUsers.Columns["ID"].Visibility = false;

Et cetera.