views:

513

answers:

1

I am using the Infragistics UltraWinGrid (version Win 9.1). The default behavior is to allow the user to type text into a cell. When one copies multiple cells from an Excel spreadsheet, only the first cell's data will be pasted into the UltraWinGrid.

One can easily change the behavior to paste multiple cells by setting the UltraWinGrid cells to be uneditable with CellClickAction.CellSelect; Unfortunately when you do this you may not type data into the cells.

So I have attempted to modify these settings with the events for InitializeLayout, KeyDown, and KeyPress.

    private void ugridQuoteSheet_InitializeLayout(object sender, InitializeLayoutEventArgs e)
    {
        e.Layout.Override.AllowMultiCellOperations = AllowMultiCellOperation.All;
        e.Layout.Override.CellClickAction = CellClickAction.CellSelect;
    }

    //Event used to circumvent the control key from choking in
    //the KeyPress event. This doesn't work btw.
    private void ugridQuoteSheet_KeyDown(object sender, KeyEventArgs e)
    {
        UltraGrid grid = (UltraGrid)sender;

        if (e.Control == true)
        {
           e.SuppressKeyPress = true;
        }
    }

    // This event comes after the KeyDown event. I made a lame attempt to stop
    // the control button with (e.KeyChar != 22). I lifted some of this from 
    // the Infragistics post: http://forums.infragistics.com/forums/p/23690/86732.aspx#86732
    private void ugridQuoteSheet_KeyPress(object sender, KeyPressEventArgs e)
    {
        UltraGrid grid = (UltraGrid)sender;
        if ((grid != null) && (grid.ActiveCell != null) && (!grid.ActiveCell.IsInEditMode) && (e.KeyChar != 22))
        {
            grid.PerformAction(UltraGridAction.EnterEditMode);
            EditorWithText editor = (EditorWithText)grid.ActiveCell.EditorResolved;
            editor.TextBox.Text = e.KeyChar.ToString();
            editor.TextBox.SelectionStart = 1;
        }
    }

    // This puts the grid in CellSelect mode again so I won't edit text.
    private void ugridQuoteSheet_AfterCellUpdate(object sender, CellEventArgs e)
    {
        this.ugridQuoteSheet.DisplayLayout.Override.CellClickAction = CellClickAction.CellSelect;
    }

I can now key in values into the cells again. The problem is, when I press [ctrl][v] for paste, the KeyPressEventArgs.KeyChar is 22 and there is no 'v'. You can see my futile attempt to circumvent this problem in the ugridQuoteSheet_KeyPress delegate. What is the right combination of event handling and CellClickAction setting to allow both Copy-Paste and typing into a cell of the UltraWinGrid?

A: 

After a bit more careful reading of the post mentioned before ( http://forums.infragistics.com/forums/p/23690/86732.aspx#86732 ) I have been able to tackle this problem.

This can be all handled within the KeyPress event after setting UltraWinGrid.DisplayLayout.Override.CellClickAction = CellClickAction.CellSelect; in the InitializeLayout event of course.

    private void ugridQuoteSheet_KeyPress(object sender, KeyPressEventArgs e)
    {
        UltraGrid grid = (UltraGrid)sender;

        if (!Char.IsControl(e.KeyChar) && grid != null && grid.ActiveCell != null &&
            grid.ActiveCell.EditorResolved is EditorWithText && !grid.ActiveCell.IsInEditMode)
        {
            grid.PerformAction(UltraGridAction.EnterEditMode);
            EditorWithText editor = (EditorWithText)grid.ActiveCell.EditorResolved;
            editor.TextBox.Text = e.KeyChar.ToString();
            editor.TextBox.SelectionStart = 1;
        }
    }

I was ignorant of how to handle the simultaneous key press, [ctrl][v]. The Char.IsControl(e.KeyChar) does the trick here.

Blanthor