Connecting Tech Pros Worldwide Help | Site Map

How to Change Column Name in DataGridView in Runtime

Newbie
 
Join Date: Sep 2008
Posts: 4
#1: Sep 29 '08
I want to change the DataGridView Heading on run time by giving in textbox.


how i will do this,plz help me

Thnx
best answer - posted by Plater
All the columns in the DataGridView have a HeaderText Property which you can set to a different string for display purposes.
All thought it is recomended to have the correct query in the first place
Newbie
 
Join Date: Sep 2008
Posts: 4
#2: Sep 30 '08

re: How to Change Column Name in DataGridView in Runtime


HI,

I want to change the columnName of DataGridView on Runtime,The datasource of DataGridView is DataTable which i fil from DataBase,

i does not want to change the name of column in query.


Plz reply
MrMancunian's Avatar
Expert
 
Join Date: Jul 2008
Location: Utrecht, The Netherlands
Posts: 274
#3: Sep 30 '08

re: How to Change Column Name in DataGridView in Runtime


Can you post a piece of code in which you initiate the gridview? Furthermore, it would be nice if you told us what language you are using...
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#4: Sep 30 '08

re: How to Change Column Name in DataGridView in Runtime


All the columns in the DataGridView have a HeaderText Property which you can set to a different string for display purposes.
All thought it is recomended to have the correct query in the first place
Newbie
 
Join Date: Sep 2008
Posts: 7
#5: Oct 1 '08

re: How to Change Column Name in DataGridView in Runtime


If you mean in VB.NET then its

DataGrid.Columns(0).HeaderText = MyTextBox.Text

and use that wherever you need to.

hope it helps
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#6: Oct 1 '08

re: How to Change Column Name in DataGridView in Runtime


THREADS MERGED.
Please do not double post your questions. It is against the posting guidelines.

MODERATOR
Newbie
 
Join Date: Nov 2009
Location: North Carolina, USA
Posts: 1
#7: 1 Week Ago

re: How to Change Column Name in DataGridView in Runtime


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


Quote:

Originally Posted by AlmightyJu View Post

If you mean in VB.NET then its

DataGrid.Columns(0).HeaderText = MyTextBox.Text

and use that wherever you need to.

hope it helps

That only lets you name the first column

EDIT: Code does not work. Please reply telling me where I went wrong
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#8: 1 Week Ago

re: How to Change Column Name in DataGridView in Runtime


If you have a valid column selected, setting the HeaderText property will change its visual style.
The code you used had no error checking, which is very bad form.
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#9: 1 Week Ago

re: How to Change Column Name in DataGridView in Runtime


Quote:

Originally Posted by jrff9173 View Post

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:
Expand|Select|Wrap|Line Numbers
  1. Dim newHeaderText As String = IIf(String.IsNullOrEmpty(TextBox2.Text), "", TextBox2.Text)
  2.  
  3. If DataGridView1.SelectedColumns IsNot Nothing AndAlso DataGridView1.SelectedColumns.Count > 0 Then
  4.   For Each selectedColumn As DataGridViewColumn In DataGridView1.SelectedColumns
  5.     selectedColumn.HeaderText = newHeaderText
  6.   Next
  7. End If
-Frinny
Reply


Similar .NET Framework bytes