int RowNum = this.doubleBufferListView1.Rows.Count;//总行数 int ColumnNum = this.doubleBufferListView1.Rows[1].Cells.Count;//得到总列数 for (int i = 0; i < RowNum; i++) { for (int j = 0; j < ColumnNum; j++) { if (doubleBufferListView1.Rows[i].Cells[j].Value.ToString() == "国债") { MessageBox.Show(i.ToString()); DataGridViewCellStyle style = new DataGridViewCellStyle(); style.BackColor = Color.Red; this.doubleBufferListView1.Rows[i].DefaultCellStyle = style; } } }
在datagridview中的这个事件中写:
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { // If the column is the Artist column, check the // value. if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Artist") { if (e.Value != null) { // Check for the string "pink" in the cell. string stringValue = (string)e.Value; stringValue = stringValue.ToLower(); if ((stringValue.IndexOf("pink") > -1)) { e.CellStyle.BackColor = Color.Pink; } } } else if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Release Date") { ShortFormDateFormat(e); } }
直接给e.Value赋值,就可以改变原来的值。
http://www.telerik.com/help/winforms/gridview-cells-formatting-cells.html