Changing column headers in datagrid control

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
connelvalentineAsked:
Who is Participating?
 
Wayne Taylor (webtubbs)Connect With a Mentor Commented:
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
 
connelvalentineAuthor Commented:
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
 
connelvalentineAuthor Commented:
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
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.

 
wiswalldCommented:
Right click datagrid. Edit columns
0
 
hatem72Connect With a Mentor Commented:


Hi,

Try ( Form Load ) :
dataGridView1.Columns(1).HeaderCell.Value = "New Header Name"
0
 
hatem72Commented:
Hi,

Sorry dont put it before loading the data but after that becuase it overwrite the HeaderNames
0
 
connelvalentineAuthor Commented:
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
 
hatem72Commented:

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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.