【问题标题】:only one cell color should be change in row只有一种单元格颜色应该在行中更改
【发布时间】:2012-02-27 12:56:47
【问题描述】:

我正在使用 win 表单中的 datagridview 现在当任何用户单击该单元格时我必须更改单元格的颜色我必须将该单元格颜色更改为我使用此代码的红色

 DataGridViewCellStyle CellStyle = new DataGridViewCellStyle();
 CellStyle.BackColor = Color.Red;
 dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex].Style = CellStyle;

但是现在如果用户选择了同一行中的任何其他单元格,那么它的颜色应该更改为 红色和先前选择的应该再次使白色成为未选择。

【问题讨论】:

    标签: c# winforms datagridview


    【解决方案1】:

    您需要维护对先前选择的单元格的引用,以便将其更改回来。

    DataGridViewCell _currentCell = null; // class level variable
    
    ... 
    
    
    // inside your event, set the current cell back to white
    if(_currentCell != null) _currentCell.Style.BackColor = Color.White;
    
    // now set the current cell to the selected cell
    _currentCell = dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex];
    
    _currentCell.Style.BackColor = Color.Red;
    

    【讨论】:

    • 你需要创建变量'CurrentCell'
    • datagridviewcell类型的变量currentcell应该是什么类型\
    • 我想是的...如果您将鼠标悬停在“行”上,它应该会告诉您集合类型。按 F12 并转到定义,它应该会告诉您集合包含什么类型的对象。
    猜你喜欢
    • 2019-07-15
    • 2013-03-15
    • 1970-01-01
    • 2017-01-19
    • 2014-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多