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 Datas=this. Rows. Cast (). The Where (row=& gt; Row. DefaultCellStyle. BackColor==Color. LightGray). Select (row=& gt; Row. DataBoundItem). ToList ();

Base. Sort (dataGridViewColumn, direction);
This. Rows. Cast (). The Where (row=& gt; Datas. The Contains (row. DataBoundItem)) ToList (). The ForEach (row=& gt; Row. DefaultCellStyle. BackColor=Color. LightGray);

}
}

To upgrade the custom class...

CodePudding user response:

reference 1/f, rabbit party at large 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 Datas=this. Rows. Cast (). The Where (row=& gt; Row. DefaultCellStyle. BackColor==Color. LightGray). Select (row=& gt; Row. DataBoundItem). ToList ();

Base. Sort (dataGridViewColumn, direction);
This. Rows. Cast (). The Where (row=& gt; Datas. The Contains (row. DataBoundItem)) ToList (). The ForEach (row=& gt; Row. DefaultCellStyle. BackColor=Color. LightGray);

}
}

To upgrade the custom class...


Ha ha ha ha, if I said I wouldn't call this class you will kill me

CodePudding user response:

You can create a class file, paste directly into
Then compile time, interface design, can drag out this control,

The simplest way, will you see your Formxxx. Designer. Cs file, find your DataGridView definition, such as me this demo

1. Modify the definition
Private System. Windows, Forms DataGridView dataGridView1;

Instead of

Private MyDataGridView dataGridView1;

2. Modify the create
Form: private void InitializeComponent ()
Method, find the dataGridView1 create code
Enclosing dataGridView1=new System. Windows. Forms. The DataGridView ();

To:
Enclosing dataGridView1=new MyDataGridView ();

No other,
  •  Tags:  
  • C#
  • Related