Home > Net > Datagridview sort does not retain the background color
Datagridview sort does not retain the background color
Time:03-25
The following code
the for (int I=0; I & lt; DataGridView1. Columns. The Count; I++) { This. DataGridView1. Columns [I] SortMode=DataGridViewColumnSortMode. NotSortable; }
for (int i=0; I & lt; DataGridView1. Rows. The Count - 1; I++) { DataGridView1. ClearSelection (); String sqlX=string. The Empty; SqlX +="SELECT the enable the FROM XITEM WHERE LEFT (HOKAN, 2)='BS' and code='" + dataGridView1. Rows [I] Cells [r]." project code "Value. The ToString () +"' "; String SQL=Program. SqlCommandExecuteScalarReturnString1 (sqlX); { If (SQL=="0") { DataGridView1. Rows [I]. DefaultCellStyle. BackColor=Color. LightGray; } } } After click sort, the background color will not retain, so add two lines of code
for (int I=0; I & lt; DataGridView1. Columns. The Count; I++) { This. DataGridView1. Columns [I] SortMode=DataGridViewColumnSortMode. NotSortable; }
Banned the sorting function, I would now like to open the sorting, after sorting can retain the background color at the same time, how to modify the
CodePudding user response:
public class MyDataGridView: DataGridView { Protected override bool ProcessCmdKey (ref Message MSG, Keys keyData) { If (keyData=https://bbs.csdn.net/topics/=(Keys. | C Keys. Control)) { The StringBuilder builder=new StringBuilder (); DataGridViewRow lastRow=this. SelectedRows. Cast (). The Last (); Foreach (DataGridViewRow row in enclosing SelectedRows) { If (row. DefaultCellStyle. BackColor==Color. LightGray) { continue; } DataGridViewCell lastCell=row. Cells. Cast (). The Last (); Foreach (DataGridViewCell cell in a row. Cells) { Builder. Append (cell. The Value. The ToString ()); If (cell!=lastCell) { Builder. Append (" \ t "); } } If (row!=lastRow) { Builder. AppendLine (); } } Clipboard. SetText (builder. The ToString ()); return true; } Return to base. ProcessCmdKey (ref MSG, keyData); }
Public override void Sort (DataGridViewColumn DataGridViewColumn, ListSortDirection direction) { List