Click here to Skip to main content
15,886,584 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi frieds!

i took datagridview which shows all the data of my database with databaase column name.
i want to change that column name at run time
how? plz help me
Posted
Comments
Mycroft Holmes 25-Nov-13 3:22am    
You mark this question as ASP and are asking a question about a DataGridView - a winforms control. You need to clarify your requirement.

try
datagridview.HeaderText="Name";//Replace name with anything you want to display on the datagridview column
 
Share this answer
 
Comments
Gaurav Makwana 23-Dec-13 4:49am    
thank you
DataGridView has a column name attribute which is set to the underlying collections fielname but can be changed after you bind the data to the DGV (at which point the DGV has columns to manipulate.)
 
Share this answer
 
Comments
Gaurav Makwana 23-Dec-13 4:49am    
thank you
 
Share this answer
 
Comments
Gaurav Makwana 23-Dec-13 4:49am    
thank you
C#
protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[0].Text = "Name";
            e.Row.Cells[1].Text = "E-mail";
            e.Row.Cells[2].Text = "Place";
        }
    }
 
Share this answer
 
Comments
Gaurav Makwana 23-Dec-13 4:49am    
thank you thomas
hi Try this code...


C#
private void Form1_Load(object sender, EventArgs e)
       {
           DataTable dt = new DataTable();
           dt.Columns.Add("Name", typeof(string));
           dt.Columns.Add("address", typeof(string));
           dt.Rows.Add("karthik","bangalore");
           dt.Rows.Add("parthi", "gujrat");
           Dictionary<string, string> dictMapping = new Dictionary<string, string>();
           dictMapping.Add("Name", "Name-Modified");
           dictMapping.Add("address", "address-Modified");

           dataGridView1.DataSource = dt;
           foreach (DataGridViewColumn col in dataGridView1.Columns)
           {
               string colheader = col.HeaderText;
               var key = dictMapping.Keys.FirstOrDefault(k => k == colheader);
               if (key != null)
                   col.HeaderText = dictMapping[key];
           }


       }
 
Share this answer
 
Comments
Gaurav Makwana 23-Dec-13 4:49am    
thank you karthik
//you can try this code.

DataGridView.Columns["P_Name"].HeaderText="Name";
 
Share this answer
 
Comments
Gaurav Makwana 23-Dec-13 4:49am    
thank you neha

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