CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2004
    Location
    Belgium
    Posts
    16

    rename a columns title in a dataGridView

    Hi,

    I'm filling a dataGrid with an Access database. But of course I want to change tittles of the fields. It seems only fields of type "string" change name. All other columns are staing the same name as originally in the database. How is this possible ? Someone know ? Do I use wrong code ? To be sure I started a new project and a fresh dataGridView to be sure I did not touch any properties.

    The name "New" appears only on the columns that have a fieldtype: 'string'.

    Code:
        partial class Form1: Form
        {
            OleDbConnection DBConnection;
    
            private void Form1_Load(object sender, EventArgs e)
            {
                string DataSource = "C:\\1.mdb";
                DBConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" +
                                                   "Data Source=" + DataSource);
                DBConnection.Open();
                try {
                    OleDbDataAdapter Adapter = new OleDbDataAdapter("SELECT * " +
                                                                    "FROM Rx ",
                                                                    DBConnection);
                    DataSet ds = new DataSet();
                    Adapter.Fill(ds);
                    dataGrid.DataSource = ds;
                    ds.Tables[0].TableName = "Rx";
                    dataGrid.DataMember = "Rx";
                    for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
                        dataGrid.Columns[i].HeaderText = "New";
                } finally {
                    DBConnection.Close();
    rgds, Wilfried
    http://www.mestdagh.biz

  2. #2
    Join Date
    Sep 1999
    Location
    Red Wing, MN USA
    Posts
    312

    Re: rename a columns title in a dataGridView

    There isn't a Columns collection accessible from the DataGrid (at least not in VS.NET 2003),
    You access the layout of the Grid via the TableStyles and GridColumnStyles properties, i.e.
    Code:
             // Create an example DataSet
             DataSet ds = new DataSet();
             
             // Create a table with various column data types
             DataTable tbl = new DataTable("Example");
    
             tbl.Columns.Add("Field1", typeof(string));
             tbl.Columns.Add("Field2", typeof(int));
             tbl.Columns.Add("Field3", typeof(decimal));
             tbl.Columns.Add("Field4", typeof(double));
             tbl.Columns.Add("Field5", typeof(DateTime));
    
             // Plugin some data
             tbl.Rows.Add(new object[]{ "Blah..", (int)1, (decimal)2.3, (double)4.5, DateTime.Now});
             tbl.Rows.Add(new object[]{ "Blah..", (int)1, (decimal)2.3, (double)4.5, DateTime.Now});
             tbl.Rows.Add(new object[]{ "Blah..", (int)1, (decimal)2.3, (double)4.5, DateTime.Now});
             tbl.Rows.Add(new object[]{ "Blah..", (int)1, (decimal)2.3, (double)4.5, DateTime.Now});
             tbl.Rows.Add(new object[]{ "Blah..", (int)1, (decimal)2.3, (double)4.5, DateTime.Now});
    
             // Add the table to the DataSet
             ds.Tables.Add(tbl);
    
             // Bind the DataGrid to the DataSet
             dataGrid1.DataSource = ds;
             dataGrid1.DataMember = "Example";
    
             // This is the section you're most interested in:
    
             // Create a DataGridTableStyle for the table
             DataGridTableStyle ts = new DataGridTableStyle();
             ts.MappingName = "Example";
    
             // Add the style to the Grid auto-generating the Column Styles
             dataGrid1.TableStyles.Add(ts);
    
             // Enumerate the Column Styles changing the Header Text
             for( int index=0; index<ts.GridColumnStyles.Count; index++ )
                ts.GridColumnStyles[index].HeaderText = "Renamed" + index.ToString();
    Regards,

    - Aaron.
    Aaron Young
    Senior Programmer Analyst (Red Wing Software)
    Certified AllExperts Expert

  3. #3
    Join Date
    Dec 2004
    Location
    Belgium
    Posts
    16

    Re: rename a columns title in a dataGridView

    Hi Aaron,

    Many thanks for your (very complete reply). However it seems that .NET versions are not compatible with each other I use c# 2005 version..
    It has the Columns property accessible but it only seems to work half. I try your code but next problem arrive:

    DataGridView does not contain a definition for TableStyles

    now what's next ? You have other idea ?
    The strange part is still that all string fields work !!! Any explanation ?
    Last edited by wmestdagh; April 10th, 2005 at 01:24 PM.
    rgds, Wilfried
    http://www.mestdagh.biz

  4. #4
    Join Date
    Dec 2004
    Location
    Belgium
    Posts
    16

    Re: rename a columns title in a dataGridView

    Hi,

    It seems more complicated than I thought, but this code works. It is needed to first generate all columns in code, give the title to it and then connnect to the dataset.

    Code:
                    dataGrid.AutoGenerateColumns = false;
                    for (int i = 0; i < ds.Tables[0].Columns.Count; i++) {
                        DataGridViewColumn colNaam = new DataGridViewColumn();
                        colNaam.DataPropertyName = ds.Tables[0].Columns[i].ColumnName;
                        colNaam.HeaderText = i.ToString();  // the title
                        colNaam.CellTemplate = new DataGridViewTextBoxCell();
                        dataGrid.Columns.Add(colNaam);
                    }
                    dataGrid.DataSource = ds;
                    dataGrid.DataMember = "Rx";
    rgds, Wilfried
    http://www.mestdagh.biz

  5. #5
    Join Date
    Dec 2004
    Location
    Belgium
    Posts
    16

    Re: rename a columns title in a dataGridView

    Hi,

    Forgot something:

    Code:
    colNaam.Name = ds.Tables[0].Columns[i].ColumnName;
    rgds, Wilfried
    http://www.mestdagh.biz

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured