user2491383
user2491383

Reputation: 170

how datagridview cell color change on gridview cell click

when user click on gridview cell color will be change automatically if not click so don't change color of gridview How To Fix this and by Default 0 set on gridview cell

Upvotes: 0

Views: 2637

Answers (2)

naeemshah1
naeemshah1

Reputation: 142

This Is For Default Cell Value Set On Gridview Cell

   private void dataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
    {

        e.Row.Cells["ship_qty"].Value = (decimal)0;
        e.Row.Cells["packs"].Value = (decimal)1;
    }

This Is For Color Change

 private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewCellStyle CellStyle = new DataGridViewCellStyle();
            CellStyle.BackColor = Color.Red;
            dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style = CellStyle;
        }

Hope This is helpful for you thanks

Upvotes: 2

kostas ch.
kostas ch.

Reputation: 2035

Try the above

  private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        DataGridViewCellStyle CellStyle = new DataGridViewCellStyle();
        if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor == Color.Red)
        {
            CellStyle.BackColor = Color.White;
            dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style = CellStyle;
        }
        else
        {

            CellStyle.BackColor = Color.Red;
            dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style = CellStyle;
        }
    }

Upvotes: 0

Related Questions