Share via


how to disable mouse click for column headers within a datagrid.

Question

Tuesday, September 21, 2010 7:20 AM

Hi,

How do I switch off the click events for column headers?

All replies (7)

Tuesday, September 21, 2010 12:17 PM âś…Answered

In the designer, you can open the Columns editor and set the SortMode for each column.

In code, you can loop over the Columns collection and set the SortMode.


Tuesday, September 21, 2010 7:41 AM

You need to handle the click event and check if header row, then don't do anything

 

Thanks,
A.m.a.L
[MVP Visual C#]
Dot Net Goodies
Don't hate the hacker, hate the code

Tuesday, September 21, 2010 8:10 AM

Yes, but me not doing anything doesn't stop the default response to the click. I want the click to be completely ignored, as if it didn't happen. I tried adding the event handler, private void postsDataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) but this just allows me to add to what happens given a mouse click. It does not suppress the default response, "depressing the header cell". This moves the focus away from the row being edited to the header column causing my error checking event handler to fail. Ideally I'd like to use an e.cancel response to ignore the click but this is not an option. is there something akin to, protected override bool ProcessCmdKey(ref Message msg, Keys keyData) "that allows me to override an arrow key press given certain conditions by returning true", that can be used in response to click events?


Tuesday, September 21, 2010 8:17 AM

What has happened to this forum? Why are my responses now being presented as a single line of text?


Tuesday, September 21, 2010 9:33 AM

You can try setting the columns SortMode property to NotSortable.


Tuesday, September 21, 2010 11:31 AM

Sounds like it may be worth a try. I found the ColumnSortModeChanged event. How do I set the property?


Tuesday, September 21, 2010 12:56 PM

Thank you very much. You just brightened my day considerably. I'm going out before something else goes wrong.