Кнопки BackColor для DataGridView влияют только на кнопки после только кнопки C# - PullRequest
0 голосов
/ 20 апреля 2020

Я хотел установить кнопку BackColor из DataGridViewButtonColumn, чтобы я написал этот фрагмент кода,

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

        }

Он меняет цвет кнопки назад, но влияет только на некоторые кнопки ( кнопка после кнопки ), как показано на рисунке. enter image description here Надеемся решить эту проблему.

1 Ответ

3 голосов
/ 20 апреля 2020

Я предлагаю вам проверить AlternatingRowsDefaultCellStyle и установить его также:

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

Но поскольку мы хотим отредактировать задний цвет кнопок, вам лучше и проще настроить Style вот так:

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

Для дальнейшего изучения кнопок Datagridview вы также можете go здесь Изменить цвет кнопки в ячейке DataGridView

Надеюсь, это поможет вам!

Благословения,

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...