简体   繁体   English

如何清除datagridview中的列名

[英]How do I clear a column name in datagridview

I am naming each column in a datagridview using a right-click menu with all the names the user can use. 我正在使用带有用户可以使用的所有名称的右键单击菜单在datagridview中命名每一列。 As each name is used, I have disabled the right-click menu item for each name as it is selected so that the user cannot name two columns the same. 使用每个名称时,我已禁用每个名称的右键单击菜单项,因为用户无法选择两个相同的名称。 To do this, I am using a simple if statement to see if that column name exists: 为此,我使用一个简单的if语句来查看该列名称是否存在:

if (MyDataGridView.Columns["ColName"] != null)
   {
      ColName.Enabled = false;
   }

However, if the user wants to re-name a column, I am having trouble "un-naming" the column, as the above if statement returns true for both names after I re-name the column. 但是,如果用户想重命名列,则在“取消命名”列时会遇到麻烦,因为上面的if语句在重命名列后对两个名称都返回true。 Is there a way to clear a column name so that a column doesn't have multiple names associated with it? 有没有一种方法可以清除列名,以便一列没有与之关联的多个名称?

这应该工作:

MyDataGridView.Columns["ColName"].Name = string.Empty

使用DisplayName属性在您的datagridview中指定列名称会更加合适。

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

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