• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2092
  • Last Modified:

C# DataGridView Column Header Click Listener

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
InfoTechEE
Asked:
InfoTechEE
1 Solution
 
athomsfereCommented:
Have you tried

private void DataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {

        }

Open in new window

0
 
masterpassCommented:
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
 
InfoTechEEAuthor Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
IceBird26Commented:
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
 
InfoTechEEAuthor Commented:
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
 
IceBird26Commented:
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
 
InfoTechEEAuthor Commented:
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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now