By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,353 Members | 1,529 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,353 IT Pros & Developers. It's quick & easy.

How to Change Column Name in DataGridView in Runtime

P: 4
I want to change the DataGridView Heading on run time by giving in textbox.


how i will do this,plz help me

Thnx
Sep 29 '08 #1

✓ answered 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

Share this Question
Share on Google+
8 Replies


P: 4
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
Sep 30 '08 #2

MrMancunian
Expert 100+
P: 569
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...
Sep 30 '08 #3

Plater
Expert 5K+
P: 7,872
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
Sep 30 '08 #4

P: 7
If you mean in VB.NET then its

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

and use that wherever you need to.

hope it helps
Oct 1 '08 #5

Plater
Expert 5K+
P: 7,872
THREADS MERGED.
Please do not double post your questions. It is against the posting guidelines.

MODERATOR
Oct 1 '08 #6

P: 1
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


@AlmightyJu
That only lets you name the first column

EDIT: Code does not work. Please reply telling me where I went wrong
Nov 8 '09 #7

Plater
Expert 5K+
P: 7,872
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.
Nov 9 '09 #8

Frinavale
Expert Mod 5K+
P: 9,731
@jrff9173
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
Nov 9 '09 #9

Post your reply

Sign in to post your reply or Sign up for a free account.