dataGridView Column header text

Posted on 2005-05-12
Last Modified: 2008-01-09
I am initialising my dataGridView columns using this code:

 string[] sAry = r.Split(input);
                for (int i = 0; i < sAry.Length; i++)
                    DataColumn dc = new DataColumn();
                    dc.ColumnName = i.ToString();
                    dc.Caption = "test123";                                                  
                    dc.DataType = System.Type.GetType("System.Single");

I would expect to see the column header set by the .caption property, however it is displaying the .ColumnName property.

In this example I want all my column headers to display test123.

How do I set the text for column headers? taking into account that the columns are set at run time and more than one column could have the same text.

When setting the columns statically through Visual Studio it is possible to set the HeaderText property for a column, how should I define this through code?
Question by:mattlaver
    LVL 2

    Expert Comment

    Unfortunately this is correct that the grid does not automatically use the caption for the header text of the grid.

    Columns are abstracted as properties of a DataRow object. By design, the properties are named according to the value of the ColumnName property, not the value of the Caption property so that you can bind to arbitrary objects and not just to DataRow objects.

    The way to set column specific properties in the datagrid is to create the columns in the datatable and also create the columns for the datatable in the TableStyles collection.  You could do this in the designer by accessing the TableStyles collections or through code as follows:
    The following creates a dummy table, a texbox column for each one and the sets the headertext. By setting the MappingName Property of the Column you are mapping the column in the grid to the column in the table with the same name:

                                                    DataTable dt=new DataTable();
                      DataGridTableStyle style=new DataGridTableStyle();
                      for(int i=0; i<3;i++)
                            dt.Columns.Add("col " + i);
                            DataGridTextBoxColumn col=new DataGridTextBoxColumn();
                            col.MappingName="col " + i;
                                                   //add dummy rows
                      for(int i=0; i<5;i++)
                            dt.Rows.Add(new object[]{"Item " + i, "Stuff " + i, "Junk " + i});

                                                    //add the style

    LVL 1

    Author Comment

    Thanks Ruby,

    I think you have got me 90% of the way there, I will add a style property to my business object base so that its data can be described at the presentation level.

    I am using a DataGridView not a dataGrid at the presentation level (.net 2.0), the problem here is that the DataGridView does not have a TableStyles property.

    Do you know how to apply a tablestyle to the DataGridView component? my google searches are proving to be fruitless.

    Thanks in advance.


    LVL 2

    Accepted Solution

    I haven't worked much with the DataGridView , however i did notice now that you can access a DataGridView's column collection and access it's headertext property.
    This is a nice new feature in .net 2.0, you have much easier access to DataGridView cells, row, and columns and don't have to use table styles for such simple column property settings.

    The following line of code should accomplish your objective much easier since you are using .net 2.0

    dataGridView1.Columns("columnName").HeaderText = "test123"

    Here is an article I read a little while back that was a nice introduction to the new DataGridView and how to use some of it's new features.

    Good luck,

    LVL 2

    Expert Comment

    Oops, one more thing, if you are going to be applying lots of styles and were going to add a style object then pay particular attention to the section of the article titled. "DataGridView styles", You can create style objects and set them on the column  row, and cell level.  It also details the priority they are applied in.

    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    Suggested Solutions

    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now