(我为英语错误感到抱歉) 你好, 我试图从datagridview内的按钮更改颜色。我已经用来自sql Server的数据喂了dgv,它工作得很好。我得到了我需要的所有数据。然后我在索引[0]上为网格中的每一行添加了按钮,以打开用于编辑目的的第二个窗口(也可以正常工作)。但我也希望按钮可以单独更改颜色。我有一列Index [5],称为“状态”,可能有3种不同的类型:“活动”,“过期”和“过期”。 “ if cell ='active'”有效,如果正确,则进行识别。但是,变色代码不起作用。
我已经尝试过安静一些事情,并浪费了一整天。 我尝试了“ row.Cells [0] .Style.BackColor = Color.Green;”和ForeColor,将Color.Green更改为System.Drawing.Color.Green。尝试每次更改某些内容时刷新一次,如果完成则仅刷新一次。我将代码缩短为仅活动状态,并尝试仅使用活动状态的testdata,这也不起作用。我尝试使用调试器,但凭我的一点知识却找不到任何东西。我尝试了“dgvAlleAnträge.Rows[row.Index] .Cells [5] .Style.BackColor = Color.Green;”并将按钮更改为每种“平面”样式,因为有人说这可行。
`private void AlleAntraege_Load(object sender, EventArgs e)
{
try
{
string con = "Data Source=" + Properties.Settings.Default.Server_Name + "; Initial Catalog=" + Properties.Settings.Default.Name_Of_DB + ";Integrated Security=" + Properties.Settings.Default.Integrated_Security;
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(con);
SqlConnection sqlconn = new SqlConnection(builder.ToString());
string sqlquery = Properties.Settings.Default.sqlquery;
SqlCommand sqlcomm = new SqlCommand("SELECT TOP 50" + sqlquery, sqlconn);
sqlconn.Open();
SqlDataAdapter sdr = new SqlDataAdapter(sqlcomm);
DataTable dt = new DataTable();
sdr.Fill(dt);
dgvAlleAnträge.DataSource = dt;
sqlconn.Close();
dgvAlleAnträge.Columns[2].HeaderCell.Value = "Antrags ID";
dgvAlleAnträge.Columns[4].HeaderCell.Value = "In-Pro-Nr.";
dgvAlleAnträge.Columns[6].HeaderCell.Value = "Erstellungsdatum";
dgvAlleAnträge.Columns[7].HeaderCell.Value = "Antragsteller UserID";
dgvAlleAnträge.Columns[9].HeaderCell.Value = "IOW";
dgvAlleAnträge.Columns[10].HeaderCell.Value = "Datum Von";
dgvAlleAnträge.Columns[11].HeaderCell.Value = "Datum Bis";
dgvAlleAnträge.Columns[12].HeaderCell.Value = "Status Mail";
dgvAlleAnträge.Columns[13].HeaderCell.Value = "Beschreibung";
dgvAlleAnträge.Columns[14].HeaderCell.Value = "Stellvertreter UserID";
dgvAlleAnträge.Columns[15].HeaderCell.Value = "Anwender UserID";
dgvAlleAnträge.Columns[16].HeaderCell.Value = "Anwender Vorname";
dgvAlleAnträge.Columns[17].HeaderCell.Value = "Anwender Nachname";
dgvAlleAnträge.Columns[18].HeaderCell.Value = "Anwender Email";
dgvAlleAnträge.Columns[19].HeaderCell.Value = "Anwender Firma";
dgvAlleAnträge.Columns[22].HeaderCell.Value = "Von System Name";
dgvAlleAnträge.Columns[23].HeaderCell.Value = "Von System IP";
dgvAlleAnträge.Columns[25].HeaderCell.Value = "Nach System Name";
dgvAlleAnträge.Columns[26].HeaderCell.Value = "Nach System IP";
dgvAlleAnträge.Columns[5].Visible = true;
(dgvAlleAnträge.Columns[0] as DataGridViewButtonColumn).FlatStyle = FlatStyle.Flat;
foreach (DataGridViewRow row in dgvAlleAnträge.Rows)
{
if (row.Cells[5].Value.ToString().Contains("aktiv") || row.Cells[5].Value.ToString().Contains("Aktiv"))
{
(dgvAlleAnträge.Columns[0] as DataGridViewButtonColumn).FlatStyle = FlatStyle.Flat;
row.Cells[0].Style.BackColor = Color.Green;
}
}
}
catch { }
}
`
如果“状态”列中的单元格为“活动”,我希望按钮将背景色更改为绿色
答案 0 :(得分:0)
row.Cell[0].Style.BackColor = Color.Green;
不起作用。您必须使用(DataGridView1.Columns[row.Index] as (DataGridViewButtonColumn).DefaultCellStyle.BackColor = Color.Green;
,然后您的按钮才能获得所需的颜色。