Search code examples
c#winformsdatagridviewdatagridviewcolumn

Columns order issue with dataGridView in C#


I have a windows form where I am using DataGridView control. It is bind to a bindingsource at design time. Via code I am adding 3 more columns to the DataGridView control. I am first checking if these 3 columns are exists or not. If yes, I am removing those from the DataGridView and adding them back.

For the first iteration, the order of the column is proper say A, B, C. But for next iteration the order of these newly added columns changes as C, B, A.

I tried setting AutoGenerateColumns to false in the constructor. But it is not working for me. Datasource is assigned at design time.

Please help me out to resolve this issue.


Solution

  • Based on Steve's comment, I found an alternative to this issue. Now I am checking the existence of column. If column is already added to the grid, I am not removing and adding it again. This prevents the original order in which those columns are added.