DaveInIowa
Programmer
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.
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));
}
}
}