C#DataGridView Columnindex和Visual Display
有一个DataGridView表,C#语言。 例如6列组成。有必要将第七个添加到桌子的末端,而是将其添加到第三名。 但是,因此,您将不得不重写很多代码,因为列索引会发生变化。 我对这个问题感兴趣:是否可以在第三位置上视觉显示列,并将其称为7
?
屏幕:
There is a datagridview table, c# language.
Consisting, for example, of 6 columns. It is necessary to add the seventh, but not to the end of the table, but to the third place.
But because of this, you will have to rewrite a lot of code, because the column indexes will shift.
I'm interested in the question: is it possible to visually display a column in the third position, and refer to it as 7?*
*I understand that indexes start from zero and will be 2 and 6 respectively.
Screens:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题是:是否可以在第三位置上视觉显示列,并将其称为7?,答案是是YES 。
作为一个建议,最适合我的是完全不处理
int
索引,而是使用string
datagridview.columns
indexer 并使用datagridview.columns [“ columnName”]
参考各个列。这解决了当索引从插入中变化时必须重写代码的问题(如您提到的),因为您可以用名称参考列。有时您知道所有可能的列名可能是什么。在这种情况下,您可以通过将其抽象为枚举来使其更加健壮:
示例 github
在mainform中覆盖此方法:
要获得此方法:
然后单击按钮:
要获取此:
现在
col.index
和col.displayindex
是两个不同的数字,但使用该名称仍然可行。希望这给你一些想法。
Your question is: Is it possible to visually display a column in the third position, and refer to it as 7? and the answer is yes.
As a suggestion, what works best for me is to not deal with the
int
index at all, but to use thestring
indexer ofDatagridview.Columns
and usedatagridview.Columns["columnName"]
to refer to individual columns. This solves the problem of having to rewrite code when the index changes from an insert (as you mentioned) because you refer to columns by name.Sometimes you know what all the possible column names might be. In this case, you can make this even more robust by abstracting them out to an enum:
Example GitHub
Override this method in MainForm:
to get this:
then click the button:
to get this:
Now
col.Index
andcol.DisplayIndex
are two different numbers but using the name still works.Hope this give you a few ideas.