473,785 Members | 2,482 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to Change Column Name in DataGridView in Runtime

4 New Member
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
8 39947
kashifabbasi786
4 New Member
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
569 Recognized Expert Contributor
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
7,872 Recognized Expert Expert
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
AlmightyJu
7 New Member
If you mean in VB.NET then its

DataGrid.Column s(0).HeaderText = MyTextBox.Text

and use that wherever you need to.

hope it helps
Oct 1 '08 #5
Plater
7,872 Recognized Expert Expert
THREADS MERGED.
Please do not double post your questions. It is against the posting guidelines.

MODERATOR
Oct 1 '08 #6
jrff9173
1 New Member
Just figured it out.

DataGridView1.C olumns(DataGrid View1.SelectedC olumns.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
7,872 Recognized Expert Expert
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
9,735 Recognized Expert Moderator Expert
@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.C olumns(DataGrid View1.SelectedC olumns.ToString )

Actually it's more likely that this is your problem.
I'm pretty sure that DataGridView1.S electedColumns. ToString will return you "DataGridViewCo lumnCollection" .

In all likelihood you do not have a column named "DataGridViewCo lumnCollection" so when you try to set the HeaderText of this non-existent column you are getting a NullReferenceEx ception.

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

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

Similar topics

4
20157
by: mokles | last post by:
Hi All, I am trying to change column name on an existing table. I am using SQL Server 7. As the table is quite big, it is taking quite long time to do it. By the way I could change the column name only through the Enterprize Manager. Is it possible to change the column name using SQL script? Why the change of column name will depend on the size of the table?
0
290
by: Larry Pits | last post by:
Hi All, Is there any code for ASP.Net to programmatically change the server name within crystal reports? I looked for all technical document at cystal decision site, but no luck. TIA
2
2303
by: Mick White | last post by:
I inadvertantly named a field "in" mysql> select * from guestbook; +----+-------+-------+-----------------+----------------+ | id | fname | lname | comments | in | +----+-------+-------+-----------------+----------------+ | 1 | Mick | White | Test 123 | 20050208000000 | | 2 | Ann | White | Hello World | 20050213101948 | | 3 | | | It's a nice day | 20050213102405 |...
1
2305
by: ibm_97 | last post by:
DB2 8.2 on AIX Hi, I'd like to change a column's name in a table which is part of replication. This column is identity column (generated always). 1. Since DB2 will drop and recreate the table with the changed column name, do I have to do something like 'set integrity off'?
2
13382
by: tony lock | last post by:
Has anybody found a way to disable a column in DataGridView i.e prevent it being selected. With DataGrid it was fairly easy to check on the column being entered and move the selection to the next column, this does not seem possible with DataGridView.
5
2294
by: Joe | last post by:
Is there anyway to change the name or add a property to and existing class in .NET 1.1? For example if I have a property called Name and I want to change it to MyName. -Joe
3
35763
by: SimonHeffer | last post by:
I've added a DataGridView and added some columns in the designer, now I want to add some rows with data applied via column name. Having done some web trawling I've ended up with this: private void AddRow(string thing,string thingdesc) { DataGridViewRow row = new DataGridViewRow(); row.CreateCells(MyGrid); // #1 row.Cells.Value = "Add"; // #2 this makes it work MyGrid.Rows.Add(row); ...
2
1948
by: mukeshrasm | last post by:
Hi I am creating page dynamically with upload image(more than one) feature. while uploading image I don't know which images are already uploaded so I want to give user a feature to change the file name if the image file already exist. so how can I do that?
7
5006
JustJim
by: JustJim | last post by:
Hi If all you can do with ALTER TABLE....ALTER COLUMN is change the datatype/size then how do I change the name of a field? I have several database files spread all over the state and some of the users (Darn users!) have changed a field from "Datum" to "MapDatum" and I need to change it back because it is causing problems with the front end. I suspect that I'm going to have to determine if the field MapDatum exists and if it does, make...
0
10324
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10090
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9949
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6739
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.