在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
显示我如何可以加载“priorita”数据与DGV比较和更改文本颜色DGV行?我的代码不起作用。你有任何想法或解决方案?谢谢。
你能详细说一下_“这不行吗?”_?它显示没有颜色或错误的颜色?当你放置一个断点并逐步完成代码时,哪一部分被执行以及你期望执行哪一部分? –
您可以上传带描述的输出截图,以便我们了解您的要求 –
它不显示颜色(默认黑色)。 – Martin