[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

dataGridView Column header text

Posted on 2005-05-12
4
Medium Priority
?
7,317 Views
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");
                    ADataFile.dt.Columns.Add(dc);
                }

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?
0
Comment
Question by:mattlaver
  • 3
4 Comments
 
LVL 2

Expert Comment

by:rubixxcube
ID: 13998273
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();
                  style.DataGrid=dataGrid1;
                  for(int i=0; i<3;i++)
                  {
                        dt.Columns.Add("col " + i);
                        
                        DataGridTextBoxColumn col=new DataGridTextBoxColumn();
                        col.HeaderText="test123";
                        col.MappingName="col " + i;
                                  style.GridColumnStyles.Add(col);
                           }
                                               //add dummy rows
                  for(int i=0; i<5;i++)
                  {
                        dt.Rows.Add(new object[]{"Item " + i, "Stuff " + i, "Junk " + i});
                        

                  }
                  //bind
                  dataGrid1.DataSource=dt;
                                                //add the style
                  dataGrid1.TableStyles.Clear();
                  dataGrid1.TableStyles.Add(style);


HTH,
Ruby
0
 
LVL 1

Author Comment

by:mattlaver
ID: 14022103
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.

Matt

 
0
 
LVL 2

Accepted Solution

by:
rubixxcube earned 500 total points
ID: 14022603
Matt,
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.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvsdev05/html/vs05a9.asp

Good luck,
Ruby

0
 
LVL 2

Expert Comment

by:rubixxcube
ID: 14022626
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.
Ruby
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

834 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