Solved

C# DataGridView Column Header Click Listener

Posted on 2012-04-03
7
1,855 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Keyboard 2 45
Expression Evaluater 3 45
How come this regular expression allows "e"? 14 56
Why use this lambda? 12 60
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.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…

679 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