Link to home
Start Free TrialLog in
Avatar of mattlaver
mattlaver

asked on

dataGridView Column header text

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?
Avatar of rubixxcube
rubixxcube

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
Avatar of mattlaver

ASKER

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

 
ASKER CERTIFIED SOLUTION
Avatar of rubixxcube
rubixxcube

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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