1

我正在使用 Infragistics UltraWinGrid(Win 9.1 版)。默认行为是允许用户在单元格中键入文本。当从 Excel 电子表格复制多个单元格时,只有第一个单元格的数据将被粘贴到 UltraWinGrid。

通过使用CellClickAction.CellSelect将 UltraWinGrid 单元格设置为不可编辑,可以轻松更改粘贴多个单元格的行为;不幸的是,当您这样做时,您可能不会在单元格中输入数据。

因此,我尝试使用 InitializeLayout、KeyDown 和 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;
    }

我现在可以再次在单元格中键入值。问题是,当我按 [ctrl][v] 进行粘贴时,KeyPressEventArgs.KeyChar 是 22 并且没有“v”。您可以在 ugridQuoteSheet_KeyPress 委托中看到我试图规避此问题的徒劳尝试。什么是事件处理和 CellClickAction 设置的正确组合,以允许复制粘贴和键入 UltraWinGrid 的单元格?

4

1 回答 1

1

在仔细阅读之前提到的帖子(http://forums.infragistics.com/forums/p/23690/86732.aspx#86732 )之后,我已经能够解决这个问题。

这可以在设置 UltraWinGrid.DisplayLayout.Override.CellClickAction = CellClickAction.CellSelect; 之后在 KeyPress 事件中处理。当然是在 InitializeLayout 事件中。

    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.

于 2010-01-29T18:18:19.320 回答