我有多个行和列组成一个DataGridView。 我想通过每一行迭代和检查特定列的内容。 如果该列包含单词“NO”,我想改变整行红色的前景色。 下面是一些代码的尝试,但迄今为止它肯定不是工作,开始不知道如果我需要每一个细胞遍历?
码:
foreach (DataGridViewRow dgvr in dataGridView1.Rows)
{
if (dgvr.Cells["FollowedUp"].Value.ToString() == ("No"))
{
dgvr.DefaultCellStyle.ForeColor = Color.Red;
}
}
挂钩OnRowDataBound事件,然后做的东西
ASPX(网格):
<asp:.... OnRowDataBound="RowDataBound"..../>
后面的代码:
protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex == -1)
{
return;
}
if(e.Row.Cells[YOUR_COLUMN_INDEX].Text=="NO"){
e.Row.BackColor=Color.Red;
}
}
FOR的WinForms:
hook the **DataBindingComplete** event and do stuff in it:
private void dataGridView1_DataBindingComplete(object sender,
DataGridViewBindingCompleteEventArgs e)
{
if (e.ListChangedType != ListChangedType.ItemDeleted)
{
DataGridViewCellStyle red = dataGridView1.DefaultCellStyle.Clone();
red.BackColor=Color.Red;
foreach (DataGridViewRow r in dataGridView1.Rows)
{
if (r.Cells["FollowedUp"].Value.ToString()
.ToUpper().Contains("NO"))
{
r.DefaultCellStyle = red;
}
}
}
}
在您的DataGridView,处理CellFormatting事件:
dataGridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);
然后,您的事件处理程序看起来是这样的:
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if(dataGridView1.Columns[e.ColumnIndex].Name == "FollowedUp" && e.Value != null && e.Value.ToString() == "No")
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Red;
}
这样在你行不“迭代” - 简单地改变与它们涂上颜色/绘制时,他们变得可见(因此需要格式化)在网格中。
public void ColourChange()
{
DataGridViewCellStyle RedCellStyle = null;
RedCellStyle = new DataGridViewCellStyle();
RedCellStyle.ForeColor = Color.Red;
DataGridViewCellStyle GreenCellStyle = null;
GreenCellStyle = new DataGridViewCellStyle();
GreenCellStyle.ForeColor = Color.Green;
foreach (DataGridViewRow dgvr in dataGridView1.Rows)
{
if (dgvr.Cells["FollowedUp"].Value.ToString().Contains("No"))
{
dgvr.DefaultCellStyle = RedCellStyle;
}
if (dgvr.Cells["FollowedUp"].Value.ToString().Contains("Yes"))
{
dgvr.DefaultCellStyle = GreenCellStyle;
}
}
}
是否有可能有空格或其它字符的单元格的值的一部分? 如果是这样尝试使用包含方法,而不是直接的平等。
if (dgvr.Cells["FollowedUp"].Value.ToString().Contains("No"))
这是的WinForms解决方案:
private void HighlightRows()
{
DataGridViewCellStyle GreenStyle = null;
if (this.dgridv.DataSource != null)
{
RedCellStyle = new DataGridViewCellStyle();
RedCellStyle.BackColor = Color.Red;
for (Int32 i = 0; i < this.dgridv.Rows.Count; i++)
{
if (((DataTable)this.dgridv.DataSource).Rows[i]["col_name"].ToString().ToUpper() == "NO")
{
this.dgridv.Rows[i].DefaultCellStyle = RedCellStyle;
continue;
}
}
}
}
此代码工作正常,我:
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if ((string)row.Cells["property_name"].Value == UNKNOWN_PROPERTY_NAME)
{
row.DefaultCellStyle.BackColor = Color.LightSalmon;
row.DefaultCellStyle.SelectionBackColor = Color.Salmon;
}
}
除了铸造作为一个字符串,而不是调用toString我真的不看任何区别,因此可能是一个大小写错误。 尝试使用:
dgvr.Cells["FollowedUp"].Value.ToString().ToUpper() == "NO"
private void Grd_Cust_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
colorCode == 4 ? Color.Yellow : Color.Brown;
if (e.RowIndex < 0 || Grd_Cust.Rows[e.RowIndex].Cells["FollowedUp"].Value == DBNull.Value)
return;
string colorCode = Grd_Cust.Rows[e.RowIndex].Cells["FollowedUp"].Value.ToString();
e.CellStyle.BackColor = colorCode == "NO" ? Color.Red : Grd_Cust.DefaultCellStyle.BackColor;
}