Solved

C# DataGridView Column Header Click Listener

Posted on 2012-04-03
7
1,835 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
7 Comments
 
LVL 14

Accepted Solution

by:
athomsfere earned 500 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

837 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