views:

1844

answers:

2

Hi all:

I have little experience in C#.NET, and below is what I got so far:

I created a DataGridView which bound to an ObjectDataSource(a generic list, that is).

Although I can make good use of the DataGridView's CellClick event and its corresponding delegate interface(DataGridViewCellEventHandler) to do my business logic well, I am still not happy with the overall logic.

Basically the drawback of the above approach is that for some certain columns, there's no need to have a CellClick event bound to it. They are simply displaying info, not try to listen to some CellClick events.

I've tried to achieve it for quite some time. I tried to disable such columns from being able to be clicked but got no luck...

Is there a good way go around this issue?

I really don't want to check what's the actual columnIndex in my delegate handler functions and then act accordingly. Basically, if the CellClick wouldn't be triggered in the first place, then that would be an excellent solution.

Many thanks in advance!

A: 

I really don't want to check what's the actual columnIndex in my delegate handler functions and then act accordingly.

Unfortunately you don't have a choice, as the DataGridView does not expose events on its DataGridViewColumn objects. Why is this behavior problematic for you?

Ian Kemp
That's right, I cannot make the cells in particular DataGridViewColumn not respond to the CellClick events...The business logic focuses on particular cells in certain columns, I feel it is not efficient to check and filter out those "irrelevant" CellClick events caused by CellClicks from elsewhere.Say, I got Column A, B and C. I care only Column B and C, I want to ignore CellClick events triggered from Column A cells. What I am doing now is before working on anything serious, I check whether the event was coming from Column A. If so, I simply ignore it, which I feel not might be good...
Michael Mao
+1  A: 

@Ian, not entirely true.....If you really don't want to do the filtering, which I don't think is that big a deal to be honest. You can always create your own custom DataGridView and override the OnCellClick event. To make it even simpler you can set the columns you don't want to raise the event for at design time to be ReadOnly and check for that condition before raising the event.

Example:

public class MyDataGridView : DataGridView
{
     public MyDataGridView()
     {
     }

     protected override void OnCellClick(DataGridViewCellEventArgs e)
     {
          if (!Columns[e.ColumnIndex].ReadOnly)
          {
               base.OnCellClick(e);
          }
     }
}
James
Thanks So much James!That will resolve my problem in a clean and efficient way, love it!
Michael Mao