Suppose you have 3 columns in a gridview.
"ID", "Name", "Age"
Index value of these columns would be 0, 1, 2
so if you wanna remove the column "Name", you'd use.
GridView1.Columns.RemoveAt(1);
After you do that The Grid will re-arrange its columns. It will have 2 columns then.
"ID", "Age".
So technically this code won't work.
GridView1.Columns.RemoveAt(1);
GridView1.Columns.RemoveAt(2);
Index will be out of range. Cause after first line of code it has re-arranged the columns with the indexes.
GridView1.Columns.RemoveAt(1);
GridView1.Columns.RemoveAt(1);
But this will remove both "Name" and "Age" columns.
While you're using loop, remember to do i-- when the RemoveAt() condition meets.