c#datagridview键处理

编程入门 行业动态 更新时间:2024-10-27 00:33:56
本文介绍了c#datagridview键处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用DataGridView,在某些地方,我添加控制(例如textbox,combobox)

dataGridView1.Controls。添加(comboBox); comboBox.Focus();问题是,使用这个控件,并且使用ENTER命令提交选项会导致DataGridView处理键 - >之后单击进入,而不是从组合框中选择sth,datagridview中的选择更改(移动到下一个单元格)。

我使用sth:

public class MyDataGridView:DataGridView { protected override bool ProcessCmdKey(ref Message msg,Keys keyData) { if(keyData == Keys.Enter) { return true; } return base.ProcessCmdKey(ref msg,keyData); } }

但是它导致datagridview和combobox不回答到ENTER和其他键...

其他信息:我必须使用ComboBox类,而不是DataGridViewCombobox。

解决方案

尝试:

if((keyData == Keys.Enter)&&(MyComboBox.Focused))

所以DataGridView响应ENTER,除非你的控件集中。

我不确定下面的代码是否适合你的情况,但也许你可以尝试类似:

public class MyDataGridView:DataGridView { public ComboBox MyComboBox {得到;组; } //如果你没有其他方法来引用它 protected override bool ProcessCmdKey(ref Message msg,Keys keyData) { if((keyData == Keys .Enter)&&(MyComboBox.Focused)) { //提交选择逻辑 return true; } return base.ProcessCmdKey(ref msg,keyData); } }

,如果需要,对您的ComboBox的引用:

dataGridView1.Controls.Add(comboBox); dataGridView1.MyComboBox = comboBox; comboBox.Focus();

I use DataGridView, and in some places I add control to it (e.g. textbox, combobox)

dataGridView1.Controls.Add(comboBox); comboBox.Focus();

The problem is that using this control, and than commiting choice by using ENTER cause the DataGridView to "handle" the key -> after clickng enter instead of choosing sth from combobox, the selection in datagridview changes( moves to next cell).

I use sth like :

public class MyDataGridView:DataGridView { protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if(keyData == Keys.Enter) { return true; } return base.ProcessCmdKey(ref msg, keyData); } }

But it cause that datagridview and combobox doesn't answer to ENTER, and other keys...

Additional infromation: I must use ComboBox class, instead of DataGridViewCombobox. Can anyone help me how to handle ENTER in my comobox?

解决方案

Try:

if((keyData == Keys.Enter) && (MyComboBox.Focused))

so DataGridView responds to ENTER except when your control is focused.

I am not sure the following code fits your situation, but maybe you could try something like:

public class MyDataGridView:DataGridView { public ComboBox MyComboBox { get; set; } //in case you had no other way to refer to it protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if((keyData == Keys.Enter) && (MyComboBox.Focused)) { //commit choice logic return true; } return base.ProcessCmdKey(ref msg, keyData); } }

and from your form, if it needs, set the reference to your ComboBox:

dataGridView1.Controls.Add(comboBox); dataGridView1.MyComboBox = comboBox; comboBox.Focus();

更多推荐

c#datagridview键处理

本文发布于:2023-11-06 16:10:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564169.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:datagridview

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!