![](/img/trans.png)
[英]How to programmatically add data to a hierarchical Infragistics UltraWinGrid
[英]How can I emulate key press with copy & paste in an Infragistics UltraWinGrid?
我正在使用Infragistics UltraWinGrid(版本Win 9.1)。 默認行為是允許用戶在單元格中鍵入文本。 當一個人從Excel電子表格復制多個單元格時,只有第一個單元格的數據將粘貼到UltraWinGrid中。
通過將UltraWinGrid單元格設置為不可通過CellClickAction.CellSelect進行編輯,可以輕松更改粘貼多個單元格的行為; 不幸的是,當您這樣做時,可能無法在單元格中鍵入數據。
因此,我嘗試使用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的單元格?
在更仔細地閱讀了前面提到的帖子之后( 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;
}
}
我不知道如何處理同時按鍵[ctrl] [v]。 Char.IsControl(e.KeyChar)在這里起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.