usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespace_15._8datagridview Display different colors { Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } Private voidForm1_Load (Objectsender, EventArgs e) { //TODO: This line of code loads the data into the table "csharpzxwdataset.mytable001". You can move or delete it as needed. This. Mytable001tableadapter.fill ( This. csharpzxwdataset.mytable001); } Private voidDatagridview1_rowprepaint (Objectsender, Datagridviewrowprepainteventargs e) { if(E.rowindex >= DataGridView1.Rows.Count-1)return; Color Oldforecolor=NewColor (); Color Oldbackcolor=NewColor (); //object is the base class for all classes as reference types varrow =Datagridview1.rows[e.rowindex]; if(row = = Datagridview1.currentrow)//determine if the line has been manipulated { if(Row. Defaultcellstyle.forecolor! =color.white) {Oldforecolor=row. Defaultcellstyle.forecolor; Row. Defaultcellstyle.forecolor=Color.White; } if(Row. Defaultcellstyle.backcolor! =color.blue) {Oldbackcolor=row. Defaultcellstyle.backcolor; Row. Defaultcellstyle.backcolor=Color.Blue; } } Else{row. Defaultcellstyle.forecolor=Oldforecolor; Row. Defaultcellstyle.backcolor=Oldbackcolor; } } }}
15.8DataGridView Select rows Display different colors