INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

DataGridView Header Text Doesn't Change If Only the Case Changes

DataGridView Header Text Doesn't Change If Only the Case Changes

DataGridView Header Text Doesn't Change If Only the Case Changes

(OP)
I have a small problem with a program where, when I change the data source of a data grid view, if a column's header text changed only by case, the previous value is still displayed. I don't know if there's an explanation for this or if it is a bug. It is important that the heading show the actual value from the data source because I want to highlight the column header if the value is not exactly as expected, including its case.

The problem is easy enough to get around by setting the data source to null immediately before setting its actual source but I wondered if anyone could explain why this is happening so I could include it in the documentation.

The following code can be copied in its entirety to a Windows Forms application to demonstrate the behaviour I'm talking about. Clicking the 'Heading' button should display the mixed-case version of the data source. Clicking the 'HEADING' button should display the upper-case version. Clicking the 'Clear' button before clicking either of the heading buttons will display the correct value initially but then toggling back and forth between the 2 heading buttons will not change the value in the first column.

CODE

using System.Data;
using System.Windows.Forms;

namespace ChangeColumnHeaderCaseInDataGridView
{
    public partial class Form1 : Form
    {
        private DataGridView dataGridView1;
        private Button btnClear;
        private Button btnHeaderTextMixedCase;
        private Button btnHeaderTextUpperCase;

        private DataTable dtHeaderTextMixedCase;
        private DataTable dtHeaderTextUpperCase;

        public Form1()
        {
            InitializeComponent();

            dataGridView1 = new DataGridView
            {
                Location = new System.Drawing.Point(12, 12),
                Size = new System.Drawing.Size(560, 309)
            };

            btnClear = new Button {Location = new System.Drawing.Point(12, 327), Text = "Clear"};
            btnClear.Click += (s, e) => dataGridView1.DataSource = null;

            btnHeaderTextMixedCase = new Button {Location = new System.Drawing.Point(93, 327), Text = "Heading"};
            btnHeaderTextMixedCase.Click += (s, e) => dataGridView1.DataSource = dtHeaderTextMixedCase;

            btnHeaderTextUpperCase = new Button {Location = new System.Drawing.Point(174, 327), Text = "HEADING"};
            btnHeaderTextUpperCase.Click += (s, e) => dataGridView1.DataSource = dtHeaderTextUpperCase;

            ClientSize = new System.Drawing.Size(584, 362);
            Controls.Add(btnHeaderTextUpperCase);
            Controls.Add(btnHeaderTextMixedCase);
            Controls.Add(btnClear);
            Controls.Add(dataGridView1);

            dtHeaderTextMixedCase = new DataTable();
            dtHeaderTextMixedCase.Columns.Add("Heading", typeof (string));
            dtHeaderTextMixedCase.Columns.Add(@"< Should be 'Heading'", typeof(string));

            dtHeaderTextUpperCase = new DataTable();
            dtHeaderTextUpperCase.Columns.Add("HEADING", typeof (string));
            dtHeaderTextUpperCase.Columns.Add(@"< S/B 'HEADING'", typeof(string));
        }
    }
} 

RE: DataGridView Header Text Doesn't Change If Only the Case Changes

Things get all wonky when auto generating columns, and changing data sources. I don't have the code in front of me, but I believe the object tries to keep to the minimum refreshes by basically ignoring the fact that the case changed. You may want to consider an implementation like below if its something you definitely have to get fixed. The general practice you'll see out there though is just setting the datasource to null, and then back to a valid collection.

CODE --> C#

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
 
namespace ChangeColumnHeadersCaseInDataGridView
{
    public partial class Form1 : Form
    {
        private DataGridView dataGridView1;
        private Button btnClear;
        private Button btnHeaderTextMixedCase;
        private Button btnHeaderTextUpperCase;
 
        private DataTable dtHeaderTextMixedCase;
        private DataTable dtHeaderTextUpperCase;
 
        public Form1()
        {
            InitializeComponent();
 
            dataGridView1 = new DataGridView
            {
                Location = new System.Drawing.Point(12, 12),
                Size = new System.Drawing.Size(560, 309),
                AutoGenerateColumns = false
            };
 
            btnClear = new Button { Location = new System.Drawing.Point(12, 327), Text = "Clear" };
            btnClear.Click += (s, e) => dataGridView1.DataSource = null;
 
            btnHeaderTextMixedCase = new Button { Location = new System.Drawing.Point(93, 327), Text = "Heading" };
            btnHeaderTextMixedCase.Click += (s, e) => SetDataSource(dtHeaderTextMixedCase);
 
            btnHeaderTextUpperCase = new Button { Location = new System.Drawing.Point(174, 327), Text = "HEADING" };
            btnHeaderTextUpperCase.Click += (s, e) => SetDataSource(dtHeaderTextUpperCase);
 
            ClientSize = new System.Drawing.Size(584, 362);
            Controls.Add(btnHeaderTextUpperCase);
            Controls.Add(btnHeaderTextMixedCase);
            Controls.Add(btnClear);
            Controls.Add(dataGridView1);
 
            dtHeaderTextMixedCase = new DataTable();
            dtHeaderTextMixedCase.Columns.Add("Heading", typeof(string));
            dtHeaderTextMixedCase.Columns.Add(@"< Should be 'Heading'", typeof(string));
 
            dtHeaderTextUpperCase = new DataTable();
            dtHeaderTextUpperCase.Columns.Add("HEADING", typeof(string));
            dtHeaderTextUpperCase.Columns.Add(@"< S/B 'HEADING'", typeof(string));
        }
 
        private void SetDataSource(DataTable tbl)
        {
            dataGridView1.Rows.Clear();
            dataGridView1.ColumnCount = tbl.Columns.Count;
            for (int i = 0; i < tbl.Columns.Count; i++)
                dataGridView1.Columns[i].Name = tbl.Columns[i].ColumnName;
            foreach (DataRow row in tbl.Rows)
                dataGridView1.Rows.Add(row);
        }
    }
} 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close