简体   繁体   English

C#中dataGridView的列顺序问题

[英]Columns order issue with dataGridView in C#

I have a windows form where I am using DataGridView control. 我在使用DataGridView控件的Windows窗体中。 It is bind to a bindingsource at design time. 它在设计时绑定到绑定源。 Via code I am adding 3 more columns to the DataGridView control. 通过代码,我向DataGridView控件添加了3列。 I am first checking if these 3 columns are exists or not. 我首先检查这3列是否存在。 If yes, I am removing those from the DataGridView and adding them back. 如果是,我将从DataGridView中删除它们,然后重新添加。

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. 对于第一次迭代,列的顺序是正确的,例如A,B,C。但是对于下一次迭代,这些新添加的列的顺序将更改为C,B,A。

I tried setting AutoGenerateColumns to false in the constructor. 我尝试在构造函数中将AutoGenerateColumns设置为false。 But it is not working for me. 但这对我不起作用。 Datasource is assigned at design time. 数据源在设计时分配。

Please help me out to resolve this issue. 请帮助我解决此问题。

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. 这样可以防止添加这些列的原始顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM