简体   繁体   English

重新绑定后DataGridView列移动

[英]DataGridView column shifts after rebinding

I add a button column through the code to the datagridview after assigning the datasource, it shows correctly after loading. 分配数据源后,我将代码中的按钮列添加到datagridview中,加载后正确显示。 then 然后

  1. Add a new row and save it to the database 添加新行并将其保存到数据库
  2. Retrieve the data from the database 从数据库中检索数据
  3. Populate a datatable and assign it to the datasource of the datagridview 填充数据表并将其分配给datagridview的数据源

For some reason, the button column shifts from the last to second column? 由于某些原因,按钮列从最后一列移到第二列? Why would that happen? 为什么会这样?

Because you generating columns automatically, sure you have DataGridView.AutoGenerateColumns = true 因为您是自动生成列,所以请确保您具有DataGridView.AutoGenerateColumns = true

After you change DataSource all generated columns are removed and generated again. 更改DataSource后,将删除所有生成的列,然后再次生成。
But column added manually stay after generated columns was removed and index of manually added column become 0 or 1. 但是,在删除生成的列并且手动添加的列的索引变为0或1之后,手动添加的列会保留。
Then again generated columns added after manually added columns 然后在手动添加列之后再次添加生成的列

For solution you can add all columns you need manually in the order you need and set DataGridView.AutoGenerateColumns = false 对于解决方案,您可以按需要的顺序手动添加所有需要的列,并设置DataGridView.AutoGenerateColumns = false

Or change move button column to the place you want manually every time new DataSource is added 或在每次添加新的DataSource时将“移动按钮”列更改为所需的位置

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

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