datagridview字体颜色+ mysql

时间:2016-01-09 14:07:22

标签: c# mysql datagridview

在MySql中有8列(col1,col2,...,col8)和100行。 在DataGridView中有3列(1,2,3)。 第(8)栏“priorita”只有这个字符串:

 string a1 = "black";
 string a2 = "blue";
 string a3 = "red";

现在我需要从“priorita”列加载所有字符串并更改DGV中的文本颜色

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {

        using (MySqlConnection cnn = new MySqlConnection("Server=@@@;Database=OitDB;Uid=martin;Pwd=;"))
        {
            MySqlDataAdapter da = new MySqlDataAdapter("SELECT priorita FROM nrp", cnn);
            DataSet ds = new DataSet();
            da.Fill(ds, "nrp");
            int pocetDGV = dataGridView1.Rows.Count;
            for(int i = 0; i < pocetDGV; i++)
            {
            List<string> LISTpriorita = new List<string>();
            foreach (DataRow row in ds.Tables["nrp"].Rows)
            {

                LISTpriorita.Add(row["priorita"].ToString());
                string s = LISTpriorita[i];
                if (s == a1)
                {
                    DataGridViewRow row1 = dataGridView1.Rows[e.RowIndex];
                    row1.DefaultCellStyle.BackColor = Color.White;
                    row1.DefaultCellStyle.ForeColor = Color.Black;
                }
                else if (s == a2)
                {
                    DataGridViewRow row2 = dataGridView1.Rows[e.RowIndex];
                    row2.DefaultCellStyle.BackColor = Color.White;
                    row2.DefaultCellStyle.ForeColor = Color.Blue;
                }
                else if (s == a3)
                {
                    DataGridViewRow row3 = dataGridView1.Rows[e.RowIndex];
                    row3.DefaultCellStyle.BackColor = Color.White;
                    row3.DefaultCellStyle.ForeColor = Color.Red;
                } 
            }
        }
        dataGridView1.ColumnHeadersVisible = false;
        dataGridView1.EnableHeadersVisualStyles = false;
    }

col1,col2,col3显示在DGV

my mysql example

如何从“priorita”加载数据与DGV进行比较并更改DGV行中的文本颜色?我的代码不起作用。你有什么想法或解决方案吗?感谢。

1 个答案:

答案 0 :(得分:0)

dataGridView1.Rows[0].Cells[0].Style.SelectionForeColor = Color.Green;

选择单元格时的前色。

dataGridView1.Rows[0].Cells[0].Style.ForeColor = Color.Green;

未选择状态的前色。这是着色的基本条件。

            int rowc = dataGridView1.Rows.Count - 1;
        for (int i = 0; i <= rowc; i++)
        {
            if (dataGridView1.Rows[i].Cells[7].Value.ToString() == "a1")
            {
                for (int z = 0; z <= dataGridView1.ColumnCount - 1; z++)
                {
                    dataGridView1.Rows[i].Cells[z].Style.ForeColor = Color.Black;
                }
            }
            else if (dataGridView1.Rows[i].Cells[7].Value.ToString() == "a2")
            {
                for (int z = 0; z <= dataGridView1.ColumnCount - 1; z++)
                {
                    dataGridView1.Rows[i].Cells[z].Style.ForeColor = Color.Blue;
                }
            }
            else if (dataGridView1.Rows[i].Cells[7].Value.ToString() == "a3")
            {
                for (int z = 0; z <= dataGridView1.ColumnCount - 1; z++)
                {
                    dataGridView1.Rows[i].Cells[z].Style.ForeColor = Color.Red;
                }
            }
        }

我使用索引7作为col8,因为索引从0开始。我没有尝试但它必须工作。别忘了,在填充DGV后使用它。