who can help me??

I got a datagridview, one of column in datagridview is checkbox call Finish. So that, when the Finish checkbox is checkbox, I want certain row background color become yellow color.
when each time I run the program, it will display yellow color when that row Finish column was checked.

code to let row change color is

foreach(DataGridViewRow r in dataGridView1.SelectedRows)
{
      r.DefaultCellStyle.BackColor = Color.Yellow;
}

Here is code to show data in datagridview(call from access database)

ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["MyDBConnectionString"];
            string name = conSettings.ProviderName;
            string providerName = conSettings.ProviderName;
            string ConnectionString = conSettings.ConnectionString;




            string sql = "SELECT * FROM PDC where Customer NOT LIKE 'Zhia Yi' and Customer NOT LIKE 'Trent' Order by DeliveryDate,PONO,Item";
            OleDbConnection connection = new OleDbConnection(ConnectionString);
            connection.Open();
            sCommand = new OleDbCommand(sql, connection);


            sAdapter = new OleDbDataAdapter(sCommand);
            sBuilder = new OleDbCommandBuilder(sAdapter);
            sDs = new DataSet();
            sAdapter.Fill(sDs, "PDC");
            sTable = sDs.Tables["PDC"];
            connection.Close();

            dataGridView1.DataSource = sDs.Tables["PDC"];
            dataGridView1.ReadOnly = true;
            save_btn.Enabled = false;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

Recommended Answers

All 3 Replies

Check out this code:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.Columns.Add("col1", "Column 1");
            dataGridView1.Columns.Add("col2", "Column 2");
            DataGridViewCheckBoxColumn column3 = AddingCheckBoxColumn();
            dataGridView1.Columns.Add(column3);

            for (int i = 1; i < 10; i++)
                dataGridView1.Rows.Add(i, "Item " + i, false);

        }

        private DataGridViewCheckBoxColumn AddingCheckBoxColumn()
        {
            DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
            {
                column.HeaderText = "Column3";
                column.Name = "col3";
                column.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
                column.FlatStyle = FlatStyle.Standard;
                column.ThreeState = true;
                column.CellTemplate = new DataGridViewCheckBoxCell();
                column.CellTemplate.Style.BackColor = Color.White;
            }
            return column;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                DataGridViewCheckBoxCell check = row.Cells["col3"] as DataGridViewCheckBoxCell;
                if (Convert.ToBoolean(check.Value) == true)
                    row.DefaultCellStyle.BackColor = Color.Yellow;
            }
        }
    }

It works, and I hope it helps,
Mitja

Check out this code:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.Columns.Add("col1", "Column 1");
            dataGridView1.Columns.Add("col2", "Column 2");
            DataGridViewCheckBoxColumn column3 = AddingCheckBoxColumn();
            dataGridView1.Columns.Add(column3);

            for (int i = 1; i < 10; i++)
                dataGridView1.Rows.Add(i, "Item " + i, false);

        }

        private DataGridViewCheckBoxColumn AddingCheckBoxColumn()
        {
            DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
            {
                column.HeaderText = "Column3";
                column.Name = "col3";
                column.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
                column.FlatStyle = FlatStyle.Standard;
                column.ThreeState = true;
                column.CellTemplate = new DataGridViewCheckBoxCell();
                column.CellTemplate.Style.BackColor = Color.White;
            }
            return column;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                DataGridViewCheckBoxCell check = row.Cells["col3"] as DataGridViewCheckBoxCell;
                if (Convert.ToBoolean(check.Value) == true)
                    row.DefaultCellStyle.BackColor = Color.Yellow;
            }
        }
    }

It works, and I hope it helps,
Mitja

thank you for reply.
But I want use sql to let backcolor change to yellow.

thank you for reply.
But I want use sql to let backcolor change to yellow.

I'm get solution

DataGridViewCheckBoxCell check = row.Cells[17] as DataGridViewCheckBoxCell;
                if (Convert.ToBoolean(check.Value) == true)
                    row.DefaultCellStyle.BackColor = Color.Yellow;

17 is mean column 17 at datagridview table

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.