条件格式化单元格c#

时间:2013-09-02 11:50:20

标签: c# gridview formatting

如何将格式应用于此网格,如果status =已保存,我想格式化网格视图中的单元格,我希望此文本显示为红色,因此更加明显

// setup columns
            this.grid1.Cell(0, 0).Text = "ID";
            this.grid1.Cell(0, 1).Text = "Scrap Code";
            this.grid1.Cell(0, 2).Text = "Station";
            this.grid1.Cell(0, 3).Text = "Operator";
            this.grid1.Cell(0, 4).Text = "Product";
            this.grid1.Cell(0, 5).Text = "Status";
            this.grid1.Cell(0, 6).Text = "Date Logged";
            this.grid1.Cell(0, 7).Text = "Die";
            this.grid1.Cell(0, 8).Text = "Core Print";
            this.grid1.Cell(0, 9).Text = "Stamp";
            this.grid1.Cell(0, 10).Text = "Line";
            this.grid1.Cell(0, 11).Text = "Grid Ref";
            this.grid1.Cell(0, 12).Text = "Cast Date";
            this.grid1.Cell(0, 13).Text = "Shift";
            this.grid1.Cell(0, 14).Text = "Cast Hour";

            this.grid1.Column(14).Alignment = FlexCell.AlignmentEnum.CenterCenter;

    for (int i=0; i<arrData.Count; i++)
            {
                string[] theData = arrData[i].ToString().Split('\t');

                this.grid1.Cell(i + 1, 0).Text = theData[0];
                this.grid1.Cell(i + 1, 1).Text = theData[5];
                this.grid1.Cell(i + 1, 2).Text = theData[1];
                this.grid1.Cell(i + 1, 3).Text = theData[2];
                this.grid1.Cell(i + 1, 4).Text = theData[11];
                this.grid1.Cell(i + 1, 5).Text = theData[3];
                this.grid1.Cell(i + 1, 6).Text = theData[4].Replace("T", " "); ;
                this.grid1.Cell(i + 1, 7).Text = theData[6];
                this.grid1.Cell(i + 1, 8).Text = theData[12];
                this.grid1.Cell(i + 1, 9).Text = theData[13];
                this.grid1.Cell(i + 1, 10).Text = theData[14];
                this.grid1.Cell(i + 1, 11).Text = theData[7];
                this.grid1.Cell(i + 1, 12).Text = theData[16].Replace("T", " "); ;
                this.grid1.Cell(i + 1, 13).Text = theData[15];
                this.grid1.Cell(i + 1, 14).Text = theData[17];
            }

2 个答案:

答案 0 :(得分:1)

为什么不试试这个

           foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                string RowType = row.Cells[cellnumberofstatus].Value.ToString();

                if (RowType == "saved")
                {
                    row.DefaultCellStyle.BackColor = Color.White;
                    row.DefaultCellStyle.ForeColor = Color.Red;
                }
             }

答案 1 :(得分:0)

为什么不试试这个

        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            string RowType = row.Cells[cellnumberofstatus].Value.ToString();

            if (RowType == "saved")
            {
                row.DefaultCellStyle.BackColor = Color.White;
                row.DefaultCellStyle.ForeColor = Color.Red;
            }
         }

编辑:试试这个

  if(dataGridView1.Rows[h].Cells[number].Value == "saved")
         dataGridView1.Rows[h].DefaultCellStyle.ForeColor = Color.Red;