why can't the column be deleted?

  • Thread starter Thread starter snow
  • Start date Start date
S

snow

Hi,

I want to delete a specified column in a datagridview, but it doesn't
work. This is my code:

datagridview1.Columns.Remove(columnName)

Column columnName is not deleted, instead, it is moved to the last
column. Why?

Thanks for the help!
 
The way I have gotten around this problem is by removing the table(s)
from memory first. If the table is contained in a dataset then do
something like this:

For i As Integer = dataset1.Tables.Count - 1 To 0 Step -1
For Each tbl As DataTable In dataset1.Tables
dataset1.Tables.Remove(tbl)
Exit For
Next
Next

datagridview1.Columns.Clear

Then repopulate your datagridview with contents from another table. If
all you want to do is hide a column then do this:

datagridview1.Columns("X").Width = 0

Rich
 
Hi,

I want to delete a specified column in a datagridview, but it doesn't
work. This is my code:

datagridview1.Columns.Remove(columnName)

Column columnName is not deleted, instead, it is moved to the last
column. Why?

Thanks for the help!

Remove it from the select statment that fills the data adpater.
 
Hi,

I want to delete a specified column in a datagridview, but it doesn't
work. This is my code:

datagridview1.Columns.Remove(columnName)

Column columnName is not deleted, instead, it is moved to the last
column. Why?

Thanks for the help!

My guess is that you have DataGridView.AutoGenerateColumns set to
True. When you remove the column, the DataGridView notices that it is
short a column so it adds one at the end.

You could do one of:
Turn off AutoGenerateColumns before removing the column.
Set the column's Visible property to False instead of getting rid of
it.
 
Back
Top