Avishka Dambawinna
Avishka Dambawinna

Reputation: 1215

BackColor for DataGridView buttons is only affected to buttons after button only C#

I wanted to set the BackColor of DataGridViewButtonColumn button so I wrote this piece of code,

public void searchData(string searchString)
        {
            string sQuery = "SELECT indexno,firstname,lastname,address,gender,dob,email,faculty,mobile FROM student WHERE CONCAT(`indexno`, `firstname`, `lastname`, `address`, `gender`, `dob`, `email`, `faculty`, `mobile`) LIKE '%" + searchString + "%'";
            MySqlCommand cmd = new MySqlCommand(sQuery, db.getConnection());
            MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            gridviewtable.DataSource = table;

            // --- edit n delete buttons ---
            DataGridViewButtonColumn editBtn = new DataGridViewButtonColumn();
            DataGridViewButtonColumn delBtn = new DataGridViewButtonColumn();

            // -edit btn -
            editBtn.HeaderText = "Edit";
            editBtn.Name = "Edit";
            editBtn.Text = "Edit";
            editBtn.FlatStyle = FlatStyle.Flat;
            editBtn.DefaultCellStyle.BackColor = Color.Tomato;
            editBtn.UseColumnTextForButtonValue = true;
            editBtn.Width = 50;
            // - del btn-
            delBtn.HeaderText = "Delete";
            delBtn.Name = "Delete";
            delBtn.Text = "Delete";
            delBtn.UseColumnTextForButtonValue = true;
            delBtn.Width = 50;

            gridviewtable.Columns.Add(editBtn);
            gridviewtable.Columns.Add(delBtn);

        }

It changes the button back color, but only affected to some buttons only(button after a button) as shown in the image. enter image description here Hoping to solve this issue.

Upvotes: 0

Views: 379

Answers (1)

Tsiry Rakotonirina
Tsiry Rakotonirina

Reputation: 731

I suggest you to check for your AlternatingRowsDefaultCellStyle and set it as well:

gridviewtable.RowsDefaultCellStyle.BackColor = Color.Tomato;
gridviewtable.AlternatingRowsDefaultCellStyle.BackColor = Color.Tomato;

But because we want to edit the Buttons' Backcolor, it's better and easier for you to configure a Style like this:

//Set the Background Color
Color bgColor = Color.Tomato;
Color frColor = Color.Black;

//Set the Button Style
DataGridViewCellStyle style = new DataGridViewCellStyle
{
    BackColor = bgColor,
    ForeColor = frColor
};

//I prefer to iterate over my Dgv
foreach (DataGridViewRow row in dgvSheetList.Rows)
{
    DataGridViewButtonCell editBtn = (DataGridViewButtonCell)row.Cells[0].ButtonName;   //Cells[ColumnNumber]
    editBtn.FlatStyle = FlatStyle.Popup;    //It's easier to override use Popup
    editBtn.Style = style;      //<--- You apply the Style here
}

For further research about Datagridview buttons, you may go here as well Change Color of Button in DataGridView Cell

Hope this will help you!

Blessings,

Upvotes: 4

Related Questions