Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello All,

I have a datagridview with three columns on the windows form. Each row is colored on some basis with Red, Green and Yellow colors.

Now what I want is when I click on column headers I want to sort them according to colors. Like in ascending order the grid will show the Green rows first then Yellow and then the Red and in descending order the opposite.

How it is possible?

Awaiting your response.

Thanks
Kapil
Posted

An alternative is to create one field in the database for color code, say ColorCode, assign the values say 1 - Green, 2 - Yellow, 3 - Red.
Then based on this field, apply color to the rows of the DataGridView in CellFormatting event.
C#
private void  dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{

    if (e.RowIndex < 0 || dataGridView1.Rows[e.RowIndex]
                .Cells["ColorCode"].Value==DBNull.Value)
        return;
    int colorCode = (int)dataGridView1.Rows[e.RowIndex].Cells["ColorCode"].Value;
    e.CellStyle.BackColor = colorCode == 1 ? Color.Green : 
            colorCode == 2 ? Color.Yellow : Color.Red;
}

Then click on the column header of ColorCode column, to sort the rows according to the ColorCode.
 
Share this answer
 
v2
Comments
Kapil Waghe 19-Mar-12 15:06pm    
Hey Great, this is what I implemented. Thanks a lot for the help.
Kapil Waghe 19-Mar-12 16:25pm    
Hi,

Can you please tell me how to sort the hidden columns in grid?
ProEnggSoft 19-Mar-12 19:55pm    
Thank you. Please see my solution 2.
Member 13870676 11-Mar-19 23:18pm    
I didn't understand about assign value. How to do this in access? Please tell me.
You cannot sort the hidden column visually by clicking on the DataGridView as the column is not visible. One of the following options can be used, to sort the hidden column, using the bindingSource1 to which the DataGridView is bound.

Option1.

Use buttons, say Button1 and Button2 to sort the hidden column in ascending and descending order respectively. Then in the Click event of buttons set the Sort property of bindingSource1 as follows
C#
//Button1 click event
bindingSource1.Sort = "ColorCode";
//Button2 click event
bindingSource1.Sort = "ColorCode DESC";

Option2

By default the DataGridView sorts the column in ascending and descending order alternatively, when the header of that column is clicked. To disable this the SortMode property of Column can be set to NotSortable and then the ColumnHeaderMouseClick can be handled to sort the ColorCode column
C#
dataGridView1.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;
dataGridView1.Columns[1].SortMode = DataGridViewColumnSortMode.NotSortable;

private void dataGridView1_ColumnHeaderMouseClick(object sender, 
DataGridViewCellMouseEventArgs e) {
//If first column header is clicked sort ColorCode in ascending order
    if (e.ColumnIndex==0)
        bindingSource1.Sort = "ColorCode";
//If second column header is clicked sort ColorCode in descending order
    else if (e.ColumnIndex==1)
        bindingSource1.Sort = "ColorCode DESC";
}
 
Share this answer
 
Comments
Kapil Waghe 20-Mar-12 10:08am    
yeah ...... you are GREAT again :)

You rocked it. Thanks for this too much valuable solution.

Keep updating us with your great knowledge.
ProEnggSoft 20-Mar-12 10:50am    
Thank you very much for your keen interest in my solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900