2016-01-09 76 views
2

在MySql中是8列(col1,col2,...,col8)和100行。 在DataGridView中是3列(1,2,3)。 列(8) “priorita” 只有这个字符串:datagridview字体颜色+ mysql

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

现在我需要从 “priorita” 列装载所有字符串和更改文本颜色DGV

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
    { 

     using (MySqlConnection cnn = new MySqlConnection("[email protected]@@;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

它不显示颜色(默认黑色)。 – Martin

回答

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; 
       } 
      } 
     } 

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

+0

在您从DGV中读取的代码中,但可以是“a1,a2,a3”的列仅在MySql中不在DGV中。 – Martin

+0

我尝试选择从mysql到DGV的所有列,但仍然无法正常工作。它没有改变颜色 – Martin

+0

使一个看不见的列,并给无形列的索引写“细胞[7]” – Ranork