?
Solved

Changing column headers in datagrid control

Posted on 2007-07-31
8
Medium Priority
?
281 Views
Last Modified: 2013-12-25
Hi,

I've bound my datagrid control to a dataset in my VB.NET application.
The datagrid is displaying the details perfectly. The data adapater is connecting to a SQL database.

Users will be expected to modify data in the data grid control. However, due to standards established, my field names in the database's table are fld_usercode, fld_username, fld_useremail etc.
Therefore, the column hearders of the datagrid control also display these field names. I want to be able to modify the column headers of the datagrid control with more user friendly names while being bound to the USERS table. Is this possible?

BR,
CV
0
Comment
Question by:connelvalentine
8 Comments
 
LVL 48

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 800 total points
ID: 19599985
Hello connelvalentine,

You can modify the columns in the SELECT statement of the DataAdapter....

SELECT fld_usercode AS UserCode, fld_username AS UserName, fld_useremail As UserEmail FROM YourTable

This will give the columns new names, but will still be able to update the sql db.

Regards,

Wayne
0
 

Author Comment

by:connelvalentine
ID: 19600261
Hi webtubbs,

I've tried this, but as i open the form i get the following error:

An unhandled exception of type 'System.Data.ConstraintException' occurred in system.data.dll

Additional information: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.

I've basically used the VB.NET interface to modify the SQL adapter, and i've entered alias names for each of my fields. This has modified the SQL statement accordingly. Unfortunately after doing so, i'm getting the above error. Removing the aliases enables the form to open again and display the data on the grid.

Any thoughts?
0
 

Author Comment

by:connelvalentine
ID: 19600278
oh yes, forgot to mention that this line in my code is highlighted under the form load event:
sqlda_users.Fill(Ds_users, "tbl_users")
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 13

Expert Comment

by:wiswalld
ID: 19600344
Right click datagrid. Edit columns
0
 
LVL 6

Assisted Solution

by:hatem72
hatem72 earned 200 total points
ID: 19603194


Hi,

Try ( Form Load ) :
dataGridView1.Columns(1).HeaderCell.Value = "New Header Name"
0
 
LVL 6

Expert Comment

by:hatem72
ID: 19603371
Hi,

Sorry dont put it before loading the data but after that becuase it overwrite the HeaderNames
0
 

Author Comment

by:connelvalentine
ID: 19607083
Hi Guys,

Hatem, datagrid.columns doesn't work in vb.net. I think you're referring to VB6

Actually i've found a solution to my problem.
In .NET custom columns can be done by using the TableStyles collection of the datagrid and the GridColumn Style collection in the TableStyle Collection. The TableStyle should be mapped to the table name in the dataset and one grid column style should be added for each field, mapped to the field in the table, and the custom text you'd want to appear can be placed in the "Header Text" field of the column style.

BR,
CV
0
 
LVL 6

Expert Comment

by:hatem72
ID: 19607681

Hi Connelvalentine,

First thanx for the points ... about my code it work in my vb.net 2005 app

as u c i used (dataGridView1) which is new in 2005 .

best regards
 
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month16 days, 2 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question