locked
Display Cell Value Changes in DataGridView RRS feed

  • Question

  • I uppercase changes to values in the DataGridView. I even call Invalidate() to show the uppercase version of the value. The below code does not accomplish it. What do I need to do to make it work (ie: show the uppercase version of the cell)?

    string edval = Cell.EditedFormattedValue.ToString().ToUpper();
    string preval = Cell.Value.ToString();
    if (preval == edval) return;
    Cell.Value = edval;
    dgv.Invalidate();


    Jon Jacobs, There are 10 kinds of people: those who understand binary and those who don't

    Wednesday, October 24, 2018 6:20 PM

Answers

  • Other ideas.

    Target the column index

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            DataGridView1.CellFormatting += DataGridView1_CellFormatting;
        }
    
        private void DataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == 1 && e.Value != null)
            {
                e.Value = e.Value.ToString().ToUpper();
            }
        }
    }

    Using a custom column

    using System;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
        public class DataGridViewUpperCaseTextBoxColumn : DataGridViewTextBoxColumn
        {
            public DataGridViewUpperCaseTextBoxColumn() : base()
            {
                CellTemplate = new DataGridViewUpperCaseTextBoxCell();
            }
        }
    
        public class DataGridViewUpperCaseTextBoxCell : DataGridViewTextBoxCell
        {
            public DataGridViewUpperCaseTextBoxCell() : base() { }
            public override Type EditType
            {
                get
                {
                    return typeof(DataGridViewUpperCaseTextBoxEditingControl);
                }
            }
        }
    
        public class DataGridViewUpperCaseTextBoxEditingControl : DataGridViewTextBoxEditingControl
        {
            public DataGridViewUpperCaseTextBoxEditingControl() : base()
            {
                this.CharacterCasing = CharacterCasing.Upper;
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Jon Q Jacobs Wednesday, October 24, 2018 9:08 PM
    Wednesday, October 24, 2018 8:54 PM

All replies

  • Have you tried setting a breakpoint then step thru the code and examine values?

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, October 24, 2018 6:39 PM
  • Yes I have. All values are exactly what I intended.

    when I type in abcdef into the cell, I uppercase it to ABCDEFG and assign it back to the cell value. I still see abcdef in the cell, even after the call to Invalidate() on the DataGridView.


    Jon Jacobs, There are 10 kinds of people: those who understand binary and those who don't

    Wednesday, October 24, 2018 6:52 PM
  • Other ideas.

    Target the column index

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            DataGridView1.CellFormatting += DataGridView1_CellFormatting;
        }
    
        private void DataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == 1 && e.Value != null)
            {
                e.Value = e.Value.ToString().ToUpper();
            }
        }
    }

    Using a custom column

    using System;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
        public class DataGridViewUpperCaseTextBoxColumn : DataGridViewTextBoxColumn
        {
            public DataGridViewUpperCaseTextBoxColumn() : base()
            {
                CellTemplate = new DataGridViewUpperCaseTextBoxCell();
            }
        }
    
        public class DataGridViewUpperCaseTextBoxCell : DataGridViewTextBoxCell
        {
            public DataGridViewUpperCaseTextBoxCell() : base() { }
            public override Type EditType
            {
                get
                {
                    return typeof(DataGridViewUpperCaseTextBoxEditingControl);
                }
            }
        }
    
        public class DataGridViewUpperCaseTextBoxEditingControl : DataGridViewTextBoxEditingControl
        {
            public DataGridViewUpperCaseTextBoxEditingControl() : base()
            {
                this.CharacterCasing = CharacterCasing.Upper;
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Jon Q Jacobs Wednesday, October 24, 2018 9:08 PM
    Wednesday, October 24, 2018 8:54 PM
  • Thank you! That was exactly what I needed.

    Jon Jacobs, There are 10 kinds of people: those who understand binary and those who don't

    Wednesday, October 24, 2018 9:08 PM