We help IT Professionals succeed at work.

What is the proper way to force uppercase on a column in a datagridview?

rwheeler23
rwheeler23 asked
on
I have a C# program with a datagridview with 12 columns.  On column [2] I want to force uppercase so the data will not only always display as uppercase but will be written in uppercase.

                dgvViewJobLinker.Columns[2].Width = 100;
                dgvViewJobLinker.Columns[2].ReadOnly = false;
                dgvViewJobLinker.Columns[2].HeaderText = "Transfer to Job";

What is the proper way to force uppercase on a column in a datagridview?
Comment
Watch Question

Ryan ChongSoftware Team Lead

Commented:

I want to force uppercase so the data will not only always display as uppercase but will be written in uppercase.


if you mean to update the data to upper case before the display? I think this already answered your question above?

rwheeler23President

Author

Commented:
If a user type 2136d8 i want it to immediately display 2136D8. I was able to get it to display uppercase but only after the user exited the field.
Ryan ChongSoftware Team Lead

Commented:
I was able to get it to display uppercase but only after the user exited the field.

what's the application platform that you're using? Web Form, Windows Application, etc? you probably need to hook into the onchange or keydown/keypress event to change the text to uppercase.
rwheeler23President

Author

Commented:
This is a winform. I think keypress may be the way to go.
Ryan ChongSoftware Team Lead

Commented:
My bad, it's no longer that difficult to set it up, so we just need to update the CharacterCasing property to Upper

Untitled.jpg
rwheeler23President

Author

Commented:
The problem I have is i am dealing with a column in a datagridview not a textbox. I wish this was a property of a datagridview.
Ryan ChongSoftware Team Lead

Commented:
in that case, probably the easiest way will be format the data at backend (SQL), so from front end (datagridview) just display the data.
rwheeler23President

Author

Commented:
The problem there is I use _commandbulder to write the data so I do write the data as a group. The column in question is blank to start and then the users can start filling in the  blanks. I will play around with some change event or keypress to force uppercase as the user types.
Developer
Distinguished Expert 2019
Commented:
Use the CellFormatting Event; e.g. -
private void OnCellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (e.ColumnIndex == 2)
    {
        e.Value = e.Value.ToString().ToUpper();
    }
}

Open in new window

Proof of concept -

Form1.cs -
using System.Linq;
using System.Windows.Forms;

namespace EE_Q29171565
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            dataGridView1.DataSource = Enumerable.Range(0, 15).Select(i => new Person { ID = i, Name = $"Person{i}", JobTitle = GetJobTitle(i) }).ToList();
        }

        private void OnCellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == 2)
            {
                e.Value = e.Value.ToString().ToUpper();
            }
        }

        string GetJobTitle(int i)
        {
            if (i % 5 == 0)
            {
                return "programmer";
            }
            else if (i % 4 == 0)
            {
                return "dba";
            }
            else if (i % 3 == 0)
            {
                return "quality analyst";
            }
            else if (i % 2 == 0)
            {
                return "team lead";
            }
            else
            {
                return "manager";
            }
        }
    }

    class Person
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string JobTitle { get; set; }
    }
}

Open in new window

Form1.Designer.cs -
namespace EE_Q29171565
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.SuspendLayout();
            // 
            // dataGridView1
            // 
            this.dataGridView1.AllowUserToAddRows = false;
            this.dataGridView1.AllowUserToDeleteRows = false;
            this.dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
            this.dataGridView1.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.AllCells;
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.dataGridView1.Location = new System.Drawing.Point(0, 0);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.ReadOnly = true;
            this.dataGridView1.RowHeadersVisible = false;
            this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
            this.dataGridView1.Size = new System.Drawing.Size(665, 450);
            this.dataGridView1.TabIndex = 0;
            this.dataGridView1.CellFormatting += new System.Windows.Forms.DataGridViewCellFormattingEventHandler(this.OnCellFormatting);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(665, 450);
            this.Controls.Add(this.dataGridView1);
            this.Name = "Form1";
            this.Text = "Form1";
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.DataGridView dataGridView1;
    }
}

Open in new window

Produces the following output -Capture.PNG-saige-
rwheeler23President

Author

Commented:
OK, this was helpful for the display on the screen  however what is written to the database are the lowercase values.  I have a Save button at the bottom of this dgv. I am going to put a loop under this save button to uppercase all values in column [1].  This what I got to work.

        private void dgvViewJobLinker_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            // Check the value of the e.ColumnIndex property if you want to apply this formatting only so some columns.
            if (e.ColumnIndex == 1)
            {
                e.Value = e.Value.ToString().ToUpper();
                e.FormattingApplied = true;
            }
        }
rwheeler23President

Author

Commented:
Thanks for these tips. They got me pointed in the right direction.