大家好,我想根据特定值更改单元格索引,如下所示,我在这里看到了很多文章,但是在ASP.NET中,这是Windows应用程序,我如何通过Windows桌面应用程序对此进行存档。请注意,我要更改索引的列是动态创建的。感谢动态列代码的创建
private void button3_Click(object sender, EventArgs e)
{
DataTable table = new DataTable();
adap.Fill(table);
dataGridView1.DataSource = table;
table.Columns.Add("RESULTS").Expression = "Iif(((ActualWeight >= (.96 * TargetWeight)) And (ActualWeight <= (1.04 * TargetWeight))),'GOOD''BAD'))
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[7].Value.ToString() == "BAD")
row.Cells[7].Style.ForeColor = Color.Red;
//row.Cells["RESULTS"].Style.ForeColor = Color.Red;
}
}
在向datatable添加新列时table
,需要将表绑定到datagridview(请参阅:如何在c#中将datatable绑定到datagridview),然后尝试更改颜色。
试试这个
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.ForeColor = Color.Red; //change the color to what you need
}
更新:要遍历DataGridView并检查特定列中的单元格内容,您需要类似
foreach(DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[7].Value.ToString() == "BAD")
row.Cells[7].Style.ForeColor = Color.Red;
//row.Cells["RESULTS"].Style.ForeColor = Color.Red;
}
您需要将这段代码放置在触发的事件或在用数据填充DataGridView之后调用的函数中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句