Reputation: 27
(i am sorry for english mistakes) Hello, i am trying to change the color from the buttons inside a datagridview. I have fed the dgv with data from a sql Server, which is working perfectly fine. I get all the data i need. Then i added buttons on Index[0] for each row in the grid, to open a 2nd window for editing purpose (works fine too). But i also want the buttons to change individually the color. I have a column Index[5] which is called "Status", there are 3 different kinds possible: "active" "expire" and "expired". The "if cell = 'active'" works and spots if its true. But what doesn't work is the color changing code.
I already tried quiet a few things and wasted a whole day with it. I tried "row.Cells[0].Style.BackColor = Color.Green;" and ForeColor, changed Color.Green to System.Drawing.Color.Green. Tried to refresh it each time it changes something and then just once if its finished. I shortened the code to only active and tried testdata with only actives, which doesn't work either. I tried to use the debugger, but couldn't find anything with my little knowledge. I tried "dgvAlleAnträge.Rows[row.Index].Cells[5].Style.BackColor = Color.Green;" and changed the buttons to every kind of "Flat"-Style because someone said this would work.
`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 { }
}
`
I want the buttons to change the backcolor to green if the cell in column "Status" = "active"
Upvotes: 0
Views: 1308
Reputation: 27
row.Cell[0].Style.BackColor = Color.Green;
doesn't work. You have to use (DataGridView1.Columns[row.Index] as (DataGridViewButtonColumn).DefaultCellStyle.BackColor = Color.Green;
Then your Buttons will get the color you want.
Upvotes: 0