Click here to Skip to main content
15,880,405 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
I have SQL data in a DataGridView. I want to change the DataGridView's header texts without affecting the data in the database.

Please help me.
Posted
Updated 19-Nov-21 2:32am
v2
Comments
sunandandutt 15-Jul-12 23:18pm    
Kindly Improve your Question.........
kuldeepkumartak 15-Jul-12 23:43pm    
how we can change datagridview headder name change using code in c#

Use your own column of GridView and can assign the Header text of the gridview. Go to Properties of the GridView-->Columns-->Add the column and set the DataBound to the DB Column name and Header Text Property.

And Dont forget to set the AutoGeneratedColumns property to false of the gridview

Or if you want to set your header programmatically then use following code:-
C#
GridView_Id.Columns[0].HeaderText = "HeaderName";
//This will change the GridView’s first column to "HeaderName


Best of luck.
 
Share this answer
 
v2
Comments
Mohamed Mitwalli 16-Jul-12 1:17am    
5+ Win
_Amy 20-Jul-12 7:28am    
+5! Nice Answered.!
Try this:
C#
protected void grdSearchResult_RowDataBound(object sender, GridViewRowEventArgs e)
{
     if (e.Row.RowType == DataControlRowType.Header)
     {
        for (int i = 0; i < e.Row.Cells.Count; i++)
        {
            e.Row.Cells[i].Text = "your header name";
        }
     }
}
 
Share this answer
 
Comments
Mohamed Mitwalli 16-Jul-12 1:17am    
5+ ASP.net
In the properties list of the DataGridView, click on the "..." next to the "Columns" property. That will bring up a window where you can change a bunch of stuff, including the column labels.
 
Share this answer
 
The Datagridview DGV does not have to have a numerical column number , what i have found if you add in Columns like a Date selector linking back to a date column a developer can easily get lost to which column No belongs to , just simply use the Column names, VB example below , hope this assists someone.

With DGV
            .Columns("xChk").HeaderText = "Chk"
            .Columns("xChkIcn").HeaderText = "Crypt" 
            .Columns("xFleIcn").HeaderText = "Icon" 
            .Columns("xFleChkID").HeaderText = "ECO" 
            .Columns("xFleNam").HeaderText = "File Name" 
            .Columns("xFleDesc").HeaderText = "File Description" 
            .Columns("xFleStatTxt").HeaderText = "Status"
            .Columns("xRevNo").HeaderText = "Rev No." 
            .Columns("xCngdat").HeaderText = "Date" 
            .Columns("xCngID").HeaderText = "Edited" 
            .Columns("xFleVer").HeaderText = "Version" 
            .Columns("xDrwReg").HeaderText = "Reg" 
            .Columns("xCmt").HeaderText = "Comments" 
End With
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900