简体   繁体   English

使用ColorDialog更改dataGridViewCells的背景颜色

[英]Changing Background color of dataGridViewCells with a ColorDialog

So I have a dataGridView and I want to be able to select a cell and change its color. 所以我有一个dataGridView,我希望能够选择一个单元格并更改其颜色。 This is a homework assignment in which we are required to use a ColorDialog. 这是一项家庭作业,要求我们使用ColorDialog。 I have a button at the top that brings up a ColorDialog to choose from, and I have been able to successfully change a cell's background color using this, but only a specific cell (just tested out (2,2)). 我在顶部有一个按钮,它弹出一个ColorDialog可供选择,并且我已经能够成功地使用它来更改单元格的背景颜色,但是只能更改特定的单元格(刚刚测试(2,2))。 I want it to only change the color of the cell selected. 我希望它仅更改所选单元格的颜色。 How do I let the ColorDialog know which cell is currently selected? 如何让ColorDialog知道当前选择了哪个单元格? My current code for clicking the actual button is this: 我当前用于单击实际按钮的代码是这样的:

private void changeBGColorToolStripMenuItem_Click(object sender, EventArgs e)
{
    ColorDialog cd = new ColorDialog();
    cd.ShowDialog();
    dataGridView1.Rows[2].Cells[2].Style.BackColor = cd.Color;
}

Like I said before I tested out (2,2) just to get it to work, and it does change that specific cell. 就像我在测试(2,2)之前说过的那样只是为了使其正常工作,它确实会更改该特定单元格。 I just want to know how to let this function know WHICH cell I actually have selected (or whether I have a cell selected at all!). 我只想知道如何让此函数知道我实际上选择了哪个单元格(或者是否完全选择了一个单元格!)。 I also have a CellBeginEdit and CellEndEdit functions if that helps. 如果有帮助,我也有一个CellBeginEdit和CellEndEdit函数。 Thank you! 谢谢!

Close. 关。 First I suggest you use this syntax to check for OK: 首先,我建议您使用以下语法检查OK:

        if (ColorDialog .ShowDialog() == DialogResult.OK)
           dataGridView1.Rows[2].Cells[2].Style.BackColor = ColorDialog .Color;

or, shorter: 或更短:

        if (ColorDialog .ShowDialog() == DialogResult.OK)
           dataGridView1[2][2].Style.BackColor = ColorDialog .Color;

To reference the selected cell use this: 要引用选定的单元格,请使用以下命令:

dataGridView1.SelectedCells[0]

This actually says 'the first selected cell'. 这实际上是“第一个选定的单元格”。 So you should make sure that either multiselect if off of or that you change colors for all selected cells. 因此,您应确保关闭多选功能或更改所有选定单元格的颜色。

So it will be either 所以要么

        if (ColorDialog .ShowDialog() == DialogResult.OK)
           dataGridView1.SelectedCells[0].Style.BackColor = ColorDialog 

or 要么

       if (ColorDialog .ShowDialog() == DialogResult.OK)
       {
           foreach (DataGridViewCell cell in DataGridView1.SelectedCells)
              cell.Style.BackColor = ColorDialog
       }

Also in the first case you must check that there actually is a first selected cell, ie that there are selected cells in the first place, or the reference will throw an error. 另外,在第一种情况下,你必须检查是否有其实一个先选定单元格,即有选择的细胞在首位,或参考将抛出一个错误。 Check for dataGridView1.SelectedCells.Length > 0 . 检查dataGridView1.SelectedCells.Length > 0 You decide where to put the check.. 您决定将支票放在哪里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM