向数据绑定DataGridView控件添加行 [英] Adding a row to a databound DataGridView control

查看:74
本文介绍了向数据绑定DataGridView控件添加行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 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?

推荐答案

这些只是想法,可能不是完整的答案。



您可能希望查看EditMode属性而不是AllowUserToAddRows以获取更新功能。



可能与IsCellInUpdateMode分开和/或点击事件以避免第一个字符被忽略,因为第一个字符是按任意键更新的操作,表示单元格应该更新。



这应该有助于解决这两个问题。
These are just thoughts to work with and may not be the full answer.

You might want to look at something around EditMode property instead of "AllowUserToAddRows" for update capabilities.

Might be parted with IsCellInUpdateMode and/or on click events to avoid the first char getting ignored, becuase that first char is the action of the "Press any key to update" that is saying the cell should be getting updated.

This should help out with those two issues.


我对这个论坛和其他人的回答很少。这一定是一个非常困难的问题,或者我只是期望从DataGridView控件中得到太多。

经过多个小时的试验和错误,我已经找到了一个不需要子类的解决方案。解决方案并不是我想要的,即一旦用户开始输入新添加的行,就防止另一行被附加到网格的底部,但它接近了。我使用 CellLeave 事件来关闭 AllowUserToAddRows 。当用户按Tab或Enter键将数据输入第一个单元格时,这有效地从网格中删除新添加的行,将用户留在网格新添加的(最后一行)的下一个单元格中(下面没有空行) )。不优雅,但至少大部分功能。这是代码:



I''ve had very little response to this question on this forum and others. It must be a very difficult problem, or I''m simply expecting too much from the DataGridView control.
After many hours of trial and error, I have settled on a solution which does not require subclassing. The solution is not exactly what I wanted, i.e. to prevent another another row from being appended to the bottom of the grid once the user begins typing into the newly added row, but it comes close. I use the CellLeave event to turn off AllowUserToAddRows. When the user presses Tab or Enter to enter the data into the 1st cell, this effectively removes the newly added row from the grid leaving the user in the next cell of the newly added (last) row of the grid (without an empty row underneath). Not elegant, but at least mostly functional. Here''s the code:

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控件添加行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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