This is a migrated thread and some comments may be shown as answers.

Force uppercase in grid column

2 Answers 847 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Brad Fulton
Top achievements
Rank 1
Brad Fulton asked on 07 Apr 2010, 06:04 PM

        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 Answers, 1 is accepted

Sort by
0
Jeff
Top achievements
Rank 1
answered on 21 Dec 2011, 08:58 PM
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.

0
danparker276
Top achievements
Rank 2
answered on 09 Apr 2013, 11:50 PM
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
    }
}

Tags
GridView
Asked by
Brad Fulton
Top achievements
Rank 1
Answers by
Jeff
Top achievements
Rank 1
danparker276
Top achievements
Rank 2
Share this question
or