Question

I am naming each column in a datagridview using a right-click menu with all the names the user can use. 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 (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. Is there a way to clear a column name so that a column doesn't have multiple names associated with it?

Was it helpful?

Solution

This should work:

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

OTHER TIPS

It will be more appropriate to use DisplayName attribute to specify the column name in your datagridview....

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top