views:

36

answers:

2

Hi! In my winforms app, I have a UserControl that contains a DataGridView. I instantiate and load this UserControl when needed into a panel in my Main Form (frmMain). My problem is figuring out how to resond to or listen for events raised in my UC's DataGridView. For example, I want to handle the CellDoubleClick event of the DataGridView in my Main Form rather than through the UC.

Is this possible? I had thought of updating a property when the cell in the grid is double-clicked, and then let my Main form do whatever when that property changes - therefore I thought of using INotifyPropertyChanged. Im not heavily clued up on how to use it in m scenario however, and would deeply appreciate some help in this regard, or if anyone can suggest an alternate solution.

Much thanx!

+1  A: 

Your user control must encapsulate some logic, so if you want to handle event of the DataGridView that is in your control the way you've described, you probably missing something in idea of user controls and encapsulation. Technically here two ways to do this:

  1. Make a public property in your user control of type DataGridView.
  2. Make an event wrapper. You will need to create an event in your user control that is raised when DataGridView CellDoubleClick (or any) is rased and in your calling code you will handle this event wrapper.

The second approach is more logical, cos internal logic of your control is incapsulated and you can provide end-user of you component with more logical and meaningful event then CellDoubleClidk or else.

Restuta
A: 

Hi Restuta, thank u 4 your reply. Sorry for not responding earlier. I did manage to sort this issue out by creating a public event in my UC:

public event DataGridViewCellEventHandler GridRowDoubleClick {
    add { dgvTasks.CellDoubleClick += value; }
    remove { dgvTasks.CellDoubleClick -= value; }
}

and in my main form, after I instantiate and load the UC

_ucTask.GridRowDoubleClick += new DataGridViewCellEventHandler(TasksGrid_CellDoubleClick);

with the following attached event:

private void TasksGrid_CellDoubleClick( object sender, DataGridViewCellEventArgs e ) {
    // do work here!
}

This does work, although I don't know if any of u experts out there foresee a problem with this approach.

Shalan
This is the same I wrote in point 2, nice to understand that we both come with this approach independently =).
Restuta