Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

DaveInIowa

Programmer
Dec 2, 2003
576
US
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));
        }
    }
}
 
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:
[COLOR=#0000FF]using[/color] System.Collections.Generic;
[COLOR=#0000FF]using[/color] System.ComponentModel;
[COLOR=#0000FF]using[/color] System.Data;
[COLOR=#0000FF]using[/color] System.Windows.Forms;
 
[COLOR=#0000FF]namespace[/color] ChangeColumnHeadersCaseInDataGridView
{
    [COLOR=#0000FF]public[/color] [COLOR=#0000FF]partial[/color] [COLOR=#0000FF]class[/color] [COLOR=#2B91AF]Form1[/color] : [COLOR=#2B91AF]Form[/color]
    {
        [COLOR=#0000FF]private[/color] [COLOR=#2B91AF]DataGridView[/color] dataGridView1;
        [COLOR=#0000FF]private[/color] [COLOR=#2B91AF]Button[/color] btnClear;
        [COLOR=#0000FF]private[/color] [COLOR=#2B91AF]Button[/color] btnHeaderTextMixedCase;
        [COLOR=#0000FF]private[/color] [COLOR=#2B91AF]Button[/color] btnHeaderTextUpperCase;
 
        [COLOR=#0000FF]private[/color] [COLOR=#2B91AF]DataTable[/color] dtHeaderTextMixedCase;
        [COLOR=#0000FF]private[/color] [COLOR=#2B91AF]DataTable[/color] dtHeaderTextUpperCase;
 
        [COLOR=#0000FF]public[/color] Form1()
        {
            InitializeComponent();
 
            dataGridView1 = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]DataGridView[/color]
            {
                Location = [COLOR=#0000FF]new[/color] System.Drawing.[COLOR=#2B91AF]Point[/color](12, 12),
                Size = [COLOR=#0000FF]new[/color] System.Drawing.[COLOR=#2B91AF]Size[/color](560, 309),
                AutoGenerateColumns = [COLOR=#0000FF]false[/color]
            };
 
            btnClear = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]Button[/color] { Location = [COLOR=#0000FF]new[/color] System.Drawing.[COLOR=#2B91AF]Point[/color](12, 327), Text = [COLOR=#A31515]&quot;Clear&quot;[/color] };
            btnClear.Click += (s, e) =&gt; dataGridView1.DataSource = [COLOR=#0000FF]null[/color];
 
            btnHeaderTextMixedCase = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]Button[/color] { Location = [COLOR=#0000FF]new[/color] System.Drawing.[COLOR=#2B91AF]Point[/color](93, 327), Text = [COLOR=#A31515]&quot;Heading&quot;[/color] };
            btnHeaderTextMixedCase.Click += (s, e) =&gt; SetDataSource(dtHeaderTextMixedCase);
 
            btnHeaderTextUpperCase = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]Button[/color] { Location = [COLOR=#0000FF]new[/color] System.Drawing.[COLOR=#2B91AF]Point[/color](174, 327), Text = [COLOR=#A31515]&quot;HEADING&quot;[/color] };
            btnHeaderTextUpperCase.Click += (s, e) =&gt; SetDataSource(dtHeaderTextUpperCase);
 
            ClientSize = [COLOR=#0000FF]new[/color] System.Drawing.[COLOR=#2B91AF]Size[/color](584, 362);
            Controls.Add(btnHeaderTextUpperCase);
            Controls.Add(btnHeaderTextMixedCase);
            Controls.Add(btnClear);
            Controls.Add(dataGridView1);
 
            dtHeaderTextMixedCase = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]DataTable[/color]();
            dtHeaderTextMixedCase.Columns.Add([COLOR=#A31515]&quot;Heading&quot;[/color], [COLOR=#0000FF]typeof[/color]([COLOR=#0000FF]string[/color]));
            dtHeaderTextMixedCase.Columns.Add([COLOR=#800000]@&quot;&lt; Should be &#39;Heading&#39;&quot;[/color], [COLOR=#0000FF]typeof[/color]([COLOR=#0000FF]string[/color]));
 
            dtHeaderTextUpperCase = [COLOR=#0000FF]new[/color] [COLOR=#2B91AF]DataTable[/color]();
            dtHeaderTextUpperCase.Columns.Add([COLOR=#A31515]&quot;HEADING&quot;[/color], [COLOR=#0000FF]typeof[/color]([COLOR=#0000FF]string[/color]));
            dtHeaderTextUpperCase.Columns.Add([COLOR=#800000]@&quot;&lt; S/B &#39;HEADING&#39;&quot;[/color], [COLOR=#0000FF]typeof[/color]([COLOR=#0000FF]string[/color]));
        }
 
        [COLOR=#0000FF]private[/color] [COLOR=#0000FF]void[/color] SetDataSource([COLOR=#2B91AF]DataTable[/color] tbl)
        {
            dataGridView1.Rows.Clear();
            dataGridView1.ColumnCount = tbl.Columns.Count;
            [COLOR=#0000FF]for[/color] ([COLOR=#0000FF]int[/color] i = 0; i &lt; tbl.Columns.Count; i++)
                dataGridView1.Columns&#91;i&#93;.Name = tbl.Columns&#91;i&#93;.ColumnName;
            [COLOR=#0000FF]foreach[/color] ([COLOR=#2B91AF]DataRow[/color] row [COLOR=#0000FF]in[/color] tbl.Rows)
                dataGridView1.Rows.Add(row);
        }
    }
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top