3

I have a button on which I have to Call an event of Gridview can anyone help how can I do this functionality. my code is below what can do ?

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
}
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
Sheldon
  • 39
  • 2
  • 8

2 Answers2

0

You can't raise another classes events. Events are really written as a private delegate. But you can do it via Reflection or Control Inheritance.

In Control Inheritance you must be first inherit from your target controls (DataGridView) and so define a method to call that Event which you need to run it.

In other way, you simply need to find the private delegate field, get it, then invoke it:

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 });
            }
        }
    }

References:

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#?

Community
  • 1
  • 1
Behzad
  • 3,502
  • 4
  • 36
  • 63
  • Thank you @Behzad for directives ,but Im not sure how to implement your idea – Sheldon Mar 23 '15 at 17:50
  • To call your event of a control first define a **static class** in your project and then write Raise method within that class. So pass the parameters to that: **source** ='your control (dataGridView)' , **eventName** ='Name of an Event' , **eventArs** ='Eevent Arguments of your called event' – Behzad Mar 24 '15 at 09:23
  • For instance ==> public void simcard() { var args = new DataGridViewCellEventArgs( columnIndex; rowIndex); dataGridView2_CellContentClick(dataGridView2, args); }?? – Sheldon Mar 24 '15 at 12:24
  • Do you mean that: public void simcard() { var args = new DataGridViewCellEventArgs( columnIndex; rowIndex); dataGridView2.Raise("CellContentClick", args); } – Behzad Mar 24 '15 at 14:09
  • Yes I did that but it would not accept the parameters, kept saying undeclared items , that measn I would have have sort of and encapsulated variables which are not thesame like the ones in mt grdview Method. – Sheldon Mar 24 '15 at 14:42
  • Hey @Behzad,I finally figured a way around this without having to use or call the method by using expressions,but thanks for all your help :) – Sheldon Mar 24 '15 at 16:09
0

For example, I have made a static class by name 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 });
                }
            }
        }
    }
}

Then, in view layer codes and for example on the button click events raise that:

private void button1_Click(object sender, EventArgs e)
    {
        dataGridView1.Raise("Click", new EventArgs());
    }
Behzad
  • 3,502
  • 4
  • 36
  • 63