DataGridView根据条件给单元格绑定图片
代码区:
private void Form1_Load(object sender, EventArgs e) { myClass.mySqliteAPI conn = new myClass.mySqliteAPI(); //查询语句(A队上场的) string sqlCommandString = "SELECT * FROM V_CompetitionPlayer WHERE TeamID=1 AND CompetitionID=391"; DataSet dsTeamA = conn.GetData(sqlCommandString); DataView dv1 = dsTeamA.Tables[0].DefaultView; dataGridView1.AutoGenerateColumns = false;//此行指定不需要自动绑定数据列,必须放在绑定数据之前,放到后面是没用的 dataGridView1.DataSource = dv1; this.dataGridView1.ClearSelection();//默认不选中任何一行 this.dataGridView1.TabStop = false;//默认不选中任何一行 for (int i = 0; i < dataGridView1.Rows.Count; i++) { if (this.dataGridView1.Rows[i].Cells["column3"].Value.ToString() == "0")//判断条件 { this.dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Green;//根据条件给行变色 this.dataGridView1.Rows[i].Cells["column1"].Value = imageList1.Images[0]; } } }
效果图: