【问题标题】:How to call a Gridview cell click Event from button Click如何从按钮单击调用 Gridview 单元格单击事件
【发布时间】:2015-03-22 15:43:43
【问题描述】:

我有一个按钮,我必须在其上调用 Gridview 的事件,谁能帮助我如何执行此功能。我的代码在下面 能做什么?

DataGridViewTextBoxColumn col1 = new DataGridViewTextBoxColumn();
DataGridViewTextBoxColumn col2 = new DataGridViewTextBoxColumn();
DataGridViewTextBoxColumn col3 = new DataGridViewTextBoxColumn();
col1.HeaderText = "Variance";
col2.HeaderText = "Tolerance";
col3.HeaderText = "Flags";

if (e.ColumnIndex == 5 || e.ColumnIndex == 6)
{
    double  ActualWeight;
    double TargetWeight;

    object val1 = dataGridView2.Rows[e.RowIndex].Cells[5].Value;
    object val2 = dataGridView2.Rows[e.RowIndex].Cells[6].Value;

    if (val1 != null && val2 != null
      && double.TryParse(val1.ToString(), out ActualWeight)
      && double.TryParse(val2.ToString(), out TargetWeight))
    {
        dataGridView2.Rows[e.RowIndex].Cells[0].Value =Math.Round (ActualWeight-TargetWeight);
    }
    else
    {
        dataGridView2.Rows[e.RowIndex].Cells[0].Value =         "Invalid Numbers";
    }       
}


protected void bUpdate_Click(object sender, EventArgs e)
{
   // I have to call Gridview Event here
}

【问题讨论】:

    标签: c# .net winforms


    【解决方案1】:

    您不能引发其他类事件。事件实际上是作为私人委托编写的。但是您可以通过 ReflectionControl Inheritance 来实现。

    在控件继承中,您必须首先从目标控件 (DataGridView) 继承,然后定义一个方法来调用您需要运行它的事件。

    另外,你只需要找到私有委托字段,获取它,然后调用它:

    public static void Raise<TEventArgs>(this object source, string eventName, TEventArgs eventArgs) where TEventArgs : EventArgs
        {
            var eventDelegate = (MulticastDelegate)source.GetType().GetField(eventName, BindingFlags.Instance | BindingFlags.NonPublic).GetValue(source);
            if (eventDelegate != null)
            {
                foreach (var handler in eventDelegate.GetInvocationList())
                {
                    handler.Method.Invoke(handler.Target, new object[] { source, eventArgs });
                }
            }
        }
    

    参考资料:

    The following code example shows how to load an assembly, create an instance of a type from the assembly, create an event handler using a dynamic assembly, and hook up the dynamic event handler. All actions are performed using late binding.

    How to: Hook Up a Delegate Using Reflection

    How do I raise an event via reflection in .NET/C#?

    【讨论】:

    • 感谢@Behzad 的指示,但我不确定如何实现您的想法
    • 要调用您的控件事件,首先在您的项目中定义一个静态类,然后在该类中编写Raise 方法。所以将参数传递给它: source ='your control (dataGridView)' , eventName ='Name of an Event' , eventArs ='您调用的事件的 Eevent 参数'
    • 例如 ==> public void simcard() { var args = new DataGridViewCellEventArgs( columnIndex; rowIndex); dataGridView2_CellContentClick(dataGridView2, args); }??
    • 你的意思是:public void simcard() { var args = new DataGridViewCellEventArgs( columnIndex; rowIndex); dataGridView2.Raise("CellContentClick", args); }
    • 是的,我这样做了,但它不接受参数,一直说未声明的项目,这意味着我会有一些与 mt grdview 方法中的不同的封装变量。跨度>
    【解决方案2】:

    例如,我创建了一个名为 MySharedClass 的静态类:

    using System.Reflection;
    
    namespace WindowsFormsApplication1
    {
        public static class MySharedClass
        {
            public static void Raise<TEventArgs>(this object source, string eventName, TEventArgs eventArgs) where TEventArgs : EventArgs
            {
                var eventDelegate = (MulticastDelegate)source.GetType().GetField(eventName, BindingFlags.Instance | BindingFlags.NonPublic).GetValue(source);
                if (eventDelegate != null)
                {
                    foreach (var handler in eventDelegate.GetInvocationList())
                    {
                        handler.Method.Invoke(handler.Target, new object[] { source, eventArgs });
                    }
                }
            }
        }
    }
    

    然后,在视图层代码中,例如在按钮单击事件中引发:

    private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.Raise("Click", new EventArgs());
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-19
      • 1970-01-01
      • 2011-12-02
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      相关资源
      最近更新 更多