开发者

Windows Forms: DataGridView Problem with backgroundcolor after sorting

开发者 https://www.devze.com 2023-03-12 00:21 出处:网络
I have a Windows Forms datagridview, where the ro开发者_如何学Cws have different background-colors.

I have a Windows Forms datagridview, where the ro开发者_如何学Cws have different background-colors.

The problem is, that the background-colors disappear after sorting (click on a row header) and all rows are again white (default color). What could be the reason for this problem?


According to this bug report submitted to Microsoft on June 2005 this is by design for Visual Studio 2005:

Thank you for your bug report. The behavior you notice is by design. Sorting a databound grid causes all rows to be recreated (called a ListChangedType.Reset). This causes your formatting to be lost. You need to use the DataBindingComplete event to apply styles and check for the ListChangedType.Reset to know when to apply your styling. Alternatively you can use the CellFormatting event. Ideally all your formatting can be done inside the CellFormatting since it is applied dynamically.


Use Sorted event and you can restore backcolor of your datagridview.

private void datagridview_Sorted(object sender, EventArgs e)
{
    //you can restore backcolor of datagridview in this function.
   //example given below
    foreach (DataGridViewRow r in datagridview.Rows)
        r.DefaultCellStyle.BackColor = Color.FromArgb(220, 0, 0);            
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号