Quote:
Originally Posted by jrff9173
Just figured it out.
DataGridView1.Columns(DataGridView1.SelectedColumn s.ToString).HeaderText = TextBox2.Text
Where:
Datagridview1 is your datagridview control
and Textbox2 is your Control to name the column
That only lets you name the first column
EDIT: Code does not work. Please reply telling me where I went wrong
You probably went wrong where you are setting the HeaderText.
I'm not sure what you are trying to do but if the TextBox2.Text does not have any Text in it you could be referring to Null/Nothing which will throw a NullException error.
Or You may have gone wrong accessing your column....does the following column exist?
DataGridView1.Columns(DataGridView1.SelectedColumn s.ToString)
Actually it's more likely that this is your problem.
I'm pretty sure that DataGridView1.SelectedColumns.ToString will return you "DataGridViewColumnCollection".
In all likelihood you do not have a column named "DataGridViewColumnCollection" so when you try to set the HeaderText of this non-existent column you are getting a NullReferenceException.
You probably want something like the following:
-
Dim newHeaderText As String = IIf(String.IsNullOrEmpty(TextBox2.Text), "", TextBox2.Text)
-
-
If DataGridView1.SelectedColumns IsNot Nothing AndAlso DataGridView1.SelectedColumns.Count > 0 Then
-
For Each selectedColumn As DataGridViewColumn In DataGridView1.SelectedColumns
-
selectedColumn.HeaderText = newHeaderText
-
Next
-
End If
-Frinny