views:

186

answers:

1

I have a GridView with 10 columns. On a certain condition, I want to add a new column called "Expiration Date". The problem is that when the user presses "Search" again (Postback) the column is added again.
I check before adding the column, to see if it already exists:

BoundField dtExp = new BoundField
                            {DataField = "DateTimeExpired", HeaderText = "Expiration Date", DataFormatString = "{0:d}"};
if (!grid.Columns.Contains(dtExp)){grid.Columns.Add(dtExp);}

But the problem is that even if the column already exists, "Contains" returns false.
What am I doing wrong?

Thanks!

+3  A: 

It is checking whether the grid contains your new column (which obviously it won't; you haven't added it yet); you actually want to check whether it contains a different column with the same name. Perhaps just loop over the Columns, checking for one with DataField == "DateTimeExpired".

Marc Gravell
Thanks, it works - but isn't there a more "built in" solution for this?
Nir
@Nir; nope. Generally columns are only added once so it isn't an issue and doesn't need extra code.
Marc Gravell
Ok, thanks a lot, Marc.
Nir