问题描述
我有一个名为 uxContactEmailAddressesGrd
的DataGridView控件。它是绑定到电子邮件地址对象集合的数据。它有3列:
1)隐藏:电子邮件主键(PK)(int)
2)显示:电子邮件地址(字符串)
3)显示:电子邮件类型(组合框, ValueMember
= int, DisplayMember
=字符串)
我已将网格的 AllowUserToAddRows
属性设置为 False
以防止用户意外添加新的电子邮件地址。他们必须 DoubleClick
网格才能添加新行。我尝试使用 uxContactEmailAddressesGrd.Rows.Add
,但这不适用于数据绑定网格。所以我通过在网格的<$ c中将网格的 AllowUserToAddRows
属性设置为 True
来添加新行$ c> DoubleClick 事件并强制编辑新行中的第一个新单元格。这很好用,并且在网格的末尾添加了一个新行,但是当我开始键入第一个单元格时,当前(新)行下面会出现一个新行。我不想要这个,因为它可能会让我的用户感到困惑。我想我可以通过在按下某个键后将其设置回 False
来关闭 AllowUserToAddRows
属性。遗憾的是,由于单元格处于编辑模式,因此不会触发Grid的 KeyDown
事件。在C#论坛上进行了一些搜索之后,我发现了一篇文章,建议我应该继承网格控件,我做了(参见下面的代码)。
I have a DataGridView control named uxContactEmailAddressesGrd
. It is data bound to a collection of email address objects. It has 3 columns:
1) Hidden: email Primary Key (PK) (int)
2) shown: email address (string)
3) Shown: email type (combobox, ValueMember
=int, DisplayMember
=String)
I''ve set the AllowUserToAddRows
property of the grid to False
to prevent the user from accidentally adding a new email address. They have to DoubleClick
the grid to add a new row. I tried using uxContactEmailAddressesGrd.Rows.Add
, but this is not allowed for databound grids. So I add a new row by setting AllowUserToAddRows
property of the grid to True
in the grid''s DoubleClick
event and force editing of the 1st new cell in the new row. This works fine and a new row is added to the end of the grid, but as soon as I start typing into the first cell, a new row appears under the current (new) row. I don''t want this, as it may confuse my users. I figure I can simply turn off the AllowUserToAddRows
property by setting it back to False
as soon as a key is pressed. Unfortunately the Grid''s KeyDown
event is not triggered because the cell is in Edit Mode. After a bit of hunting in the C# forums, I found an article that advised I should subclass the grid control, which I did (see the code below).
public sealed class XDataGridView : DataGridView
{
private bool _singleUpdateOnly = true;
public bool SingleUpdateOnly
{
get { return _singleUpdateOnly; }
set { _singleUpdateOnly = value; }
}
[Description("Disallows user adding rows as soon as a key is struck to ensure no blank row at bottom of grid")]
protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
{
if (SingleUpdateOnly)
{
AllowUserToAddRows = false;
}
return base.ProcessCmdKey(ref msg, keyData);
}
}
这似乎可以解决一个小问题:我在单元格中键入的第一个字符总是被吞下up,即我必须按两次单词的第一个字符,才能在单元格中显示。
我做错了什么导致这个?我以为我继续用输入的字符调用基类来舔它,但似乎没有。有人可以帮忙解决这个问题吗?如何阻止它吞下第一个字符类型?
This seems to work fine with one slight problem: The 1st char I type in the cell is always swallowed up, i.e. I have to press the 1st char of a word twice for it to be shown in the cell.
What have I done wrong to cause this? I thought I had it licked by continuing to call the base class with the entered char, but seemingly not. Can anyone help with this problem? How do I stop it from swallowing the 1st char type?
推荐答案
private void uxContactEmailAddressesGrd_CellLeave(object sender, DataGridViewCellEventArgs e)
{
uxContactEmailAddressesGrd.AllowUserToAddRows = false;
}
也许有一天,别人会遇到这个问题并找到比我更优雅的解决方案。那简直太好了。确保将解决方案发布到此站点供其他人使用。
Maybe one day someone else will come across this problem and find a solution more elegant than mine. That''d be great. Make sure you post your solution to this site for others to use.
这篇关于向数据绑定DataGridView控件添加行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!