Solved

C# DataGridView Column Header Click Listener

Posted on 2012-04-03
7
1,756 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
Comment Utility
Have you tried

private void DataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {

        }

Open in new window

0
 
LVL 21

Expert Comment

by:masterpass
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 1

Expert Comment

by:IceBird26
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
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 demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now