Force uppercase in grid column

3 posts, 0 answers
  1. Brad Fulton
    Brad Fulton avatar
    1 posts
    Member since:
    Feb 2010

    Posted 07 Apr 2010 Link to this post


            private void DataGrid_KeyDown(object sender, KeyEventArgs e)
            {
                // subroutine has its own error handling
                try
                {
                    GridViewCell cell;

                    // get the current cell, if any
                    cell = DataGrid.CurrentCell;
                    if (cell != null)
                    {
                        // if the current cell is in the CODE column...
                        if (cell.Column.Header.ToString().ToUpper() == "CODE")
                        {
                            // set any textboxes to force upper case
                            IList<TextBox> txtList = cell.ChildrenOfType<TextBox>();
                            foreach (TextBox txt in txtList)
                            {
                                txt.CharacterCasing = CharacterCasing.Upper;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    w.MessageInternal("DataGrid_KeyDown: " + ex.ToString());
                }
            }

  2. Jeff
    Jeff avatar
    100 posts
    Member since:
    Apr 2008

    Posted 21 Dec 2011 Link to this post

    It's been a while since this was posted, but I wanted to jump in in case anyone found this answer, when faced with this question.

    This solution - binding to the KeyDown event - doesn't work well.

    Two problems have already been mentioned in this thread.
    1. The first solution messed up the carat positioning, and didn't uppercase the last letter.
    2. The second solution fixed the carat positioning, but still didn't uppercase the last letter.

    The suggestion to use KeyUp, instead of KeyDown does result in uppercasing the last letter.  But there are other problems.  The way this messes about with the selection makes the content of the textbox unselectable.  If you press END, then hold down the shift key, and then press HOME, you'll select the full contents of the textbox - and when you release the HOME button, everything will be unselected.

    I fixed this by saving the SelectionStart and SelectionLength, in the event handler, and reapplying them after.  But even with this, there's a noticeable delay - something like half-a-second - after you release the key before a lower case character is replaced by its upper.

    A much better solution is to apply a CellEditTemplate to the column. Put a TextBox in the template, and set CharacterCasing="Upper" on it.

  3. UI for WPF is Visual Studio 2017 Ready
  4. danparker276
    danparker276 avatar
    389 posts
    Member since:
    Aug 2010

    Posted 09 Apr 2013 Link to this post

    FYI in case you're interested in my solution, there's sort of a thread on the silverlight page, but I still had to put a few things together.  This is what I came up with.  I'm not checking the column headers, but you can add that very easy:
    private void myGridView_PreparedCellForEdit_1(object sender, GridViewPreparingCellForEditEventArgs e)
    {
     
        var edtingElement = e.EditingElement as TextBox;
        if (edtingElement != null)
        {
            edtingElement.KeyUp += edtingElement_KeyUp;
     
        }
     
    }
     
    void edtingElement_KeyUp(object sender, KeyEventArgs e)
    {
        TextBox txt = (TextBox)sender;
        if (Keyboard.Modifiers != ModifierKeys.None || (e.Key < Key.A) || (e.Key > Key.Z))  //do not handle ModifierKeys (work for shift key)
        {
             
        }
        else
        {
           // string n = new string(new char[] { (char)e.Key });
            int nSelStart = txt.SelectionStart;
            txt.Text = txt.Text.ToUpper();
            txt.Select(nSelStart + 1, 0); //for cursortext
        }
    }

Back to Top