winforms datagridview行选择isssue
我正在执行一个功能,例如我在usercontrol
中具有datagridview
,并且它以基本形式托管。此网格
具有3列,例如ID,名称,年龄和此年龄列是combobox
控制。应用程序启动后,网格
将带有来自数据库表学生的数据。选择模式是单行明智的。首先,它将显示ID和名称,但是年龄列是空的,用户可以单击该列 - 这是combobox
控制。
假设用户填充的数据源于数据库,只有10行。用户单击第四行,并尝试从combobox
指定值。很好,没关系。现在查看当前行是第四,然后选择了该行。然后,用户尝试单击第六行。但是它突然选择了第一行[0索引]。我单击了第6行,但没有选择第六行。相反,第一行是自动选择的。
DGV_ROWENTER
是我们单击该行时发射的第一个事件。但是,在我不知道的dgv_rowenter
事件之前,还有另一个事件正在调用。
那是哪个事件?
首先,我选择一行,然后从combobox
[年龄列]中选择一个数字,此后,当我单击任何其他行时 - 当时它正在选择[突出显示]第一行[ 0th rowindex]。
如何防止这种情况?
I am doing a function like I have a DataGridView
in my UserControl
, and it is hosted in Base Form. This Grid
has 3 columns such as ID, Name, Age and this Age column is a ComboBox
control. When the application starts, the Grid
is populated with data from the database table Students. The selection mode is single Row wise. At first, it will show IDs and Names, but the Age column is empty, and the user can click on that column - which is a ComboBox
control.
Suppose that the user populated data stems from the DB, and there are only 10 rows. The User clicks on the 4th row and tries to specify a value from the ComboBox
. Fine, that is ok. Now see that the current row is 4th and that row is selected. Then the user tries to click on the 6th row. But it suddenly selects the very first row [0 index]. I clicked on 6th row, but it is not selecting the 6th row. Instead, the very first row is selected automatically.
The dgv_RowEnter
is the first event that is fired when we clicked on the row. But there is another event that is invoking before the dgv_RowEnter
event that I don't know.
Which is that event?
First, I select a row and picked a number from a ComboBox
[Age Column] and after that, when I click on any other row - at that time it is selecting [highlighting] the very first row [0th rowindex].
How to prevent this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的帖子中,您问:
您要问的问题表明
datagridview
的默认行为没有按照您想要的顺序提供所需的事件。使用dataSource
属性的数据绑定是使DGV以不错的方式进行的最快方法,这将是我的第一个建议。但是听起来您直接与DGV进行交互,这意味着您是一名高级用户,他想成为DGV发送的事件的“在线前部”。拦截selectedrow
,selectedColumn
和selectedcell
出门之前的方法,因为事件是为了制作pasterdatagridview:datagridview
并覆盖setSelectedRowcore
,setSelectedColumnCore
,并且setSelectedCellcore
方法。您将需要在form.designer.cs文件中更改两行,
而:
下面显示的控制台输出表明,如今所示的自定义类是“第一响应者”。
In your post, you ask:
The question you're asking indicates that the default behavior of
DataGridView
is not providing the events you want in the order you want them. Using data binding with theDataSource
property is the fastest way to make DGV behave in a decent manner out of the box and that would be my first suggestion. But it sounds as though you are interacting with the DGV directly which implies that you are an advanced user who wants to be "at the front of the line" for the events that DGV is sending. The way to interceptSelectedRow
,SelectedColumn
, andSelectedCell
before they go out as events is to make aCustomDataGridView : DataGridView
and override theSetSelectedRowCore
,SetSelectedColumnCore
, andSetSelectedCellCore
methods respectively.You will need to change two lines in the Form.Designer.cs file:
and:
The console output shown below demonstrates that the custom class as shown is now a "first responder" when these states occur.