?
Solved

C# DataGridView Column Header Click Listener

Posted on 2012-04-03
7
Medium Priority
?
1,967 Views
Last Modified: 2012-05-10
Beginner C# question here. I'm building a Windows Form Application. I gray out all the data and cells in the DataGridView, but when the user clicks on the column header, it automatically sorts (which is fine), and UN-GRAYS all the data cells.

I would like to be able to have a method to catch/listen for when the user clicks on the column header. Normally I would just go to my Design view and double-click on a button, and it would auto-generate the method declaration for me, but since the DataGrid does not have columns in the Designer view, I can't double-click on any column headers.

Anyways, I'm looking for code for the method declaration:

public void blahblah (event ______ something ________)
{
        runMethodToGrayOutAllCells();
}
0
Comment
Question by:InfoTechEE
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 14

Accepted Solution

by:
athomsfere earned 2000 total points
ID: 37803806
Have you tried

private void DataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {

        }

Open in new window

0
 
LVL 21

Expert Comment

by:masterpass
ID: 37803807
This is the MSDN documentation on all methods available for a datagridview

http://msdn.microsoft.com/en-us/library/9cx48ebw.aspx

and

I think you should be looking to implement onsorted method. Have a look here

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.sorted.aspx

Also have a look here:

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.columnheadermouseclick.aspx

hope this helps!
0
 

Author Comment

by:InfoTechEE
ID: 37803894
athomsfere: I have tried that and nothing happens.

masterpass: I looked into your third suggestion and it appears to be using the same method as athomsfere suggested. I'll take a look at the other two links shortly.

I'm just trying to understand why all my cells get UN-GRAYed when the user clicks and sorts by a column.

Here's to code I use to Gray them:

foreach (DataGridViewRow row in dataGrid.Rows)
{
   if (row.Cells["Sent"].Value.Equals("Yes"))
               {
                    //back color gray
                    row.DefaultCellStyle.BackColor = Color.LightGray;
                    //font color slate gray
                    row.DefaultCellStyle.ForeColor = Color.SlateGray;
                    //uncheck boxes
                    row.Cells[0].Value = false;
                    //read only on entire row
                    row.ReadOnly = true;
                }
}
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Expert Comment

by:IceBird26
ID: 37806246
Is "Sent" your first column in your datagridview ?

If it is, then your line of code below will change that value from "Yes" to "false" when you gray out the data the first time (probably on the form load event). After that, whenever the ColumnHeaderMouseClick event is called, none of the rows now have a Sent value of "Yes", since they've all been changed to "false" previously, and nothing will get grayed out.


row.Cells[0].Value = false;


It's better practice to use column names than column numbers.
0
 

Author Comment

by:InfoTechEE
ID: 37814222
I've attached a snapshot. The first column is a checkbox column, based on several factors inlcuding the last column called "Sent". Here's a bit more of the condition that grays out the rows.

                if (row.Cells["Sent"].Value.Equals("Yes"))
                {
                    //back color gray
                    row.DefaultCellStyle.BackColor = Color.LightGray;
                    //font color slate gray
                    row.DefaultCellStyle.ForeColor = Color.SlateGray;
                    //uncheck boxes
                    row.Cells[0].Value = false;
                    //read only on entire row
                    row.ReadOnly = true;
                }



                if ((row.Cells["Reporter"].Value == DBNull.Value) | row.Cells["Case  Name"].Value.ToString().Contains("TFT") |
                    row.Cells["Case Name"].Value.ToString().Contains("CRT") | !(row.Cells["Status"].Value.Equals("Confirmed")) |
                    (row.Cells["Email"].Value.Equals("")))
                {
                    //back color gray
                    row.DefaultCellStyle.BackColor = Color.LightGray;
                    //uncheck boxes
                    row.Cells[0].Value = false;
                    //read only on entire row
                    row.ReadOnly = true;
                }
snapshot.png
0
 
LVL 1

Expert Comment

by:IceBird26
ID: 37826941
In a datagridview, when a column header is clicked to sort it, the data gets sorted but the colours default back to the original colours.

Handling the ColumnHeaderMouseClick event and having your code within that method to change the colours to the way you want it should work.

If it doesn't, I suggest putting a few break points in your code and trace what happens on form load and when a column header is clicked.
0
 

Author Comment

by:InfoTechEE
ID: 37888289
The whole question is about ColumnHeaderMouseClick event.

Either I'm doing something wrong or it seems like the event is never being called.

What's the proper method definition for ColumnHeaderMouseClick event.
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question