如何将不同的EventHandlers附加到单个DataGridView的组合 [英] How to attach different EventHandlers to combos of a single DataGridView

查看:62
本文介绍了如何将不同的EventHandlers附加到单个DataGridView的组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VS2005(c#.net桌面应用程序)。当我将eventHandler添加到datagridview组合中时,它会自动将同一eventhandler添加到同一datagridview的所有其他组合中。

I am using VS2005 (c#.net desktop application). When I add an eventHandler to a combo of datagridview, it's automatically adding the same eventhandler to all other combos of the same datagridview.

我的代码:

private void dgvtstestdetail_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {

        DataGridView grid = (sender as DataGridView);



        if (grid.CurrentCell.OwningColumn == grid.Columns["gdvtstd_TestParameter"])
        {

            ComboBox cb = (e.Control as ComboBox);
            cb.SelectedIndexChanged -= new EventHandler(dvgCombo_SelectedIndexChanged);
            cb.SelectedIndexChanged += new EventHandler(dvgCombo_SelectedIndexChanged);

        }
 }

我想添加不同的事件处理程序到datagridview中的不同组合。请告诉我我该怎么做。

I want to add different event handlers to different combos in datagridview. Please tell me how can I do it.

推荐答案

默认情况下,我相信DataGridColumn为每个对象重用相同的ComboBox实例细胞。

By default, I believe that the DataGridColumn reuses the same instance of ComboBox for each cell. This is an optimization used by the grid to keep the number of created editing controls low.

最简单的方法是只有一个事件处理程序,检查要编辑的单元格,

The easiest thing is just to have one event handler, check the cell being edited, and take the appropriate action.

public void dvgCombo_SelectedIndexedChanged()
{
    if (<condition1>)
        ExecuteConditionOneLogic();

    if (<condition2>)
        ExecuteConditionTwoLogic();

}

更高级的解决方案是在自定义DataGridViewColumn上创建不共享编辑控件的实现。除非您确实具有一些可重用的功能,否则我不建议您这样做。

A more advanced solution would be to create your on custom DataGridViewColumn implementation which does not share an editing control. I wouldn't recommend this unless you really have some reusable functionality.

这篇关于如何将不同的EventHandlers附加到单个DataGridView的组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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