C#中的Datagridview导航问题 [英] Datagridview navigation problem in C#
本文介绍了C#中的Datagridview导航问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个WPF应用程序。我面临一种情况,即添加一个包含datagridview的窗体。我想按下回车键将焦点移动到datagridview中的下一个单元格。在Windows应用程序中,它使用ProcessDialogKey工作。但是它在WPF应用程序中不起作用
解决方案
我不确定你现在是否要求WPF或Windows表单的代码,这是针对WPF的代码可以如果您愿意,可以稍微编辑Windows窗体。
private void dg_PreviewKeyDown( object sender,KeyEventArgs e)
{
尝试
{
如果(e.Key == Key.Enter)
{
e.Handled = true ;
var cell = GetCell(dgIssuance,dgIssuance.Items.Count - 1 , 2 跨度>);
if (cell!= null )
{
cell。 IsSelected = true ;
cell.Focus();
dg.BeginEdit();
}
}
}
catch (例外情况)
{
MessageBox(ex .Message, 错误,MessageType.Error);
}
}
public static DataGridCell GetCell (DataGrid dg, int 行, int 列)
{
var rowContainer = GetRow(dg,row);
if (rowContainer!= null )
{
var presenter = GetVisualChild< datagridcellspresenter>(rowContainer);
if (presenter!= null )
{
// 尝试获取单元格,但可能会被虚拟化
var cell =(DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
if (cell == null )
{
// 现在尝试进入视图并检索单元格
dg.ScrollIntoView(rowContainer,dg。列[柱]);
cell =(DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
}
return cell;
}
}
返回 null ;
} < / datagridcellspresenter > < /跨度>
I have a WPF application. I face a situation to add a windows form which contains a datagridview. I want to move focus to next cell in datagridview on pressing the enter key. In a windows application it is working using the ProcessDialogKey. But it is not working in WPF application
解决方案
I am not sure whether you are asking for the code for WPF or Windows forms now, this is for WPF the code can be edited slightly for a Windows form if you wish.
private void dg_PreviewKeyDown(object sender, KeyEventArgs e) { try { if (e.Key == Key.Enter) { e.Handled = true; var cell = GetCell(dgIssuance, dgIssuance.Items.Count - 1, 2); if (cell != null) { cell.IsSelected = true; cell.Focus(); dg.BeginEdit(); } } } catch (Exception ex) { MessageBox(ex.Message, "Error", MessageType.Error); } } public static DataGridCell GetCell(DataGrid dg, int row, int column) { var rowContainer = GetRow(dg, row); if (rowContainer != null) { var presenter = GetVisualChild<datagridcellspresenter>(rowContainer); if (presenter != null) { // try to get the cell but it may possibly be virtualized var cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column); if (cell == null) { // now try to bring into view and retreive the cell dg.ScrollIntoView(rowContainer, dg.Columns[column]); cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column); } return cell; } } return null; }</datagridcellspresenter>
这篇关于C#中的Datagridview导航问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文