如何在gridview单元格上更改datagridview单元格颜色

时间:2013-06-17 06:28:09

标签: c#

  

当用户点击gridview单元格颜色时会自动更改   如果没有点击,所以不要改变gridview的颜色   如何解决此问题,并在gridview单元格上设置默认值0

2 个答案:

答案 0 :(得分:2)

  

这是在Gridview单元格上设置的默认单元格值

   private void dataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
    {

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

这是换色的

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

希望这对你有所帮助

答案 1 :(得分:0)

试试上面的

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