如何聚焦datagridview单元格 [英] How do I focus a datagridview cell

查看:109
本文介绍了如何聚焦datagridview单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagridview显示来自数据表的数据,在某些单元格中我必须提供正确的值,如果用户输入错误的值,程序应显示一条弹出消息告诉用户他提供了不可接受的数据,当用户按下OK时应该归还给那个有错误价值的单元格。



我尝试了什么:



 如果 AccProjectDT.Rows.Count = 没什么 然后 
MsgBox( 没有项目已注册此客户!
vbOKOnly + MsgBoxStyle.Information,
帐户
Dg1.CurrentCell = .Dg1.CurrentRow.Cells( 4
退出 Sub
结束 如果

解决方案

您无法将焦点设置为单元格,但您可以选择,如下所示:



 Dg1.CurrentCell.Selected = true; 





请看一看:如何将一个单元格聚焦在vb.net的datagridview中 [ ^ ]


感谢您的回复,因为昨天我正在阅读并尝试但结果是一样的,那么解决方案是什么,我更安全。


In解决方案1:



您正在将注意力与选择混合起来。 Cell永远不会集中,因为这不是一个控件。

通常,单元格被选中,而不是焦点。当它发生时,焦点控制它的单元格的 DataGridView 实例。



同时,当单元格如果选中,它可能会显示一些可以聚焦的编辑控件。但它仍然不意味着关注细胞。这是一个单独的控件,可以创建正在编辑单元格的印象。这称为就地编辑。



DataGridView 中的选择也取决于几个选项属性:

DataGridView.MultiSelect属性(System.Windows.Forms) [ ^ ],

DataGridView.SelectionMode属性(System.Windows.Forms) [ ^ ],

另见 Control.CanSelect属性(System.Window s.Forms) [ ^ ]。



现在,您的问题是您的误解,即使在获得解决方案1之后。这是事情:由于这种误解,你的问题的标题与你想要实现的不一致。在你的情况下,你真的抓住焦点;这是编辑控件的重点,但这与选择单元格没有直接关系。不幸的是,您没有展示代码示例的更广泛的上下文,在这种情况下,这是最重要的。



要解决您的问题,您应该更好地处理与就地编辑相关的事件



我最近演示了一个非常高级的代码示例;但它与你想要达到的目标没有直接关系。但是,它可以给你一些好主意:

覆盖datagridview_keypress方法 [ ^ ]。



您需要处理另一个事件, EditModeChanged 其中你可以进行验证:

DataGridView.EditModeChanged事件(System.Windows.Forms) [ ^ ]。



另一种选择处理此事件: DataGridView.CellValueChanged事件(System.Windows.Forms) [ ^ ]。



-SA

I have a datagridview showing data from data table, In some cell i have to provide the correct value, If the user enter a wrong value program should show an popup message telling the user that he provide unacceptable data, when user press OK should be returned to the cell witch have the wrong value.

What I have tried:

If AccProjectDT.Rows.Count = Nothing Then
       MsgBox("There are no Projects registered for this Customer!", 
              vbOKOnly + MsgBoxStyle.Information, 
              "Account")
       Dg1.CurrentCell = Me.Dg1.CurrentRow.Cells(4)
       Exit Sub
End If

解决方案

You can't set a focus to a cell but you can select it like this:

Dg1.CurrentCell.Selected = true;



Please take a look as well: how to focus a cell in datagridview in vb.net[^]


Thank you for your respond, Since yesterday i am reading and trying but result are the same, So what is the solution i am safer from that.


In addition to Solution 1:

You are mixing up focusing with selection. Cell is never focused, because this is not a control.
Generally, the cell is selected, not focused. When it happens, the focus control it the cell's DataGridView instance.

At the same time, when the cell is selected, it may show some edit control which can be focused. But it still doesn't mean focusing of the cell. This is the separate control which creates an impression that the cell is being edited. This is called "in-place" editing.

The selection in the DataGridView also depends on couple of option properties:
DataGridView.MultiSelect Property (System.Windows.Forms)[^],
DataGridView.SelectionMode Property (System.Windows.Forms)[^],
see also Control.CanSelect Property (System.Windows.Forms)[^].

Now, your problem is your misunderstanding, even after getting Solution 1. Here is the thing: due to this misunderstanding, the title of your question does not match what you are trying to achieve. In your case, you really grab the focus; and this is the focus of your edit control, but this is not directly related to selection of the cell. Unfortunately, you did not show wider context of your code sample, which is the most important in this case.

To solve your problem, you should rather handle events related to in-place editing.

I recently demonstrated a pretty advanced code sample; but it is not directly related to what you want to achieve. However, it can give you some good idea:
Override datagridview_keypress method[^].

You need to handle another event, EditModeChanged where you can do your validation:
DataGridView.EditModeChanged Event (System.Windows.Forms)[^].

Another option is handling this event: DataGridView.CellValueChanged Event (System.Windows.Forms)[^].

—SA


这篇关于如何聚焦datagridview单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆