【问题标题】:How to change datagridview colors based on value contained within cells如何根据单元格中包含的值更改 datagridview 颜色
【发布时间】:2015-06-11 01:03:43
【问题描述】:

大家好,我想根据如下所示的某个值更改单元格索引,我在这里看到了很多文章,但是在 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;
            }

}

【问题讨论】:

  • 那么您是想在点击时更改它还是根据值更改它?
  • 是的,先生,就像 cell[4]=='bad' ,color=='RED'
  • @Sheldon 这张桌子是什么? DataGridView 的名称是什么?
  • 名称是dataGridView1

标签: c#


【解决方案1】:

当您向数据表table 添加一个新列时,您需要将表绑定到datagridview(参考此:how to bind datatable to datagridview in c#)然后尝试更改颜色。

试试这个

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 填充数据后触发的事件或调用的函数中。

【讨论】:

  • @Saagar 你的想法引向了正确的方向,但是如果在单元格[7] 中有一个值'BAD'我想变成红色等等,我该如何应用......
  • 对象不包含“单元格”的定义
  • 你是对的,使用更新后的代码 ==>对象引用未设置为对象的实例。
  • 试试这个row.Cells[7].DefaultCellStyle.ForeColor = Color.Red;
  • 感谢@Sagaar 对“DefaultCellStyle”大喊大叫
【解决方案2】:

这里有一些示例代码展示了预期的结果

ASPX:

<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="true">
</asp:GridView>

后面的代码:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ArrayList al = new ArrayList();
                al.Add("open"); al.Add("close"); al.Add("other"); al.Add("open"); al.Add("other"); al.Add("close"); al.Add("open");
                this.GridView1.DataSource = al;
                this.GridView1.DataBind();
            }
        }

            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.Cells[0].Text == "open")
                {
                    e.Row.Cells[0].ForeColor = System.Drawing.Color.Red;
                }
                else if (e.Row.Cells[0].Text == "close")
                {
                    e.Row.Cells[0].ForeColor = System.Drawing.Color.Black;
                }
                else
                {
                    e.Row.Cells[0].ForeColor = System.Drawing.Color.Green;
                }
            }
        }

【讨论】:

    猜你喜欢
    • 2017-02-05
    • 2023-04-06
    • 1970-01-01
    • 2013-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多