datagridview计数单元格c#

时间:2017-11-24 19:06:09

标签: c# datagridview

我有问题。我需要计算我激活的细胞,它们是黄色的,但我不知道我是怎么做到的。在geniral我需要选择最多15个细胞,所以我需要计算它们,但我所有的尝试似乎都很遥远。我试图复制一个计数器,但它确实有效。请帮忙。

        dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
        //выделение только ячеек

        // создаём массив

        int[,] Array = new int[8, 10];

        byte numbers = 1;

        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                Array[i, j] = numbers;
                numbers++;
            }
        }

        dataGridView1.RowCount = 8;
        dataGridView1.ColumnCount = 10;

        // программно записываем массив и задаём стиль ячеек

        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                dataGridView1.Columns[j].Width = 30;
                dataGridView1.Rows[i].Height = 30;
                dataGridView1.Rows[i].Cells[j].Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                dataGridView1.Rows[i].Cells[j].Style.Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold);
                dataGridView1.Rows[i].Cells[j].Value = Array[i, j].ToString();       
            }   
        }          
    }

    private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e) // выделение ячеек
    {

        for (int i = 0; i < dataGridView1.SelectedCells.Count; i++)
        {
            if (dataGridView1.SelectedCells[i].Style.BackColor == Color.Yellow)
            {
                dataGridView1.SelectedCells[i].Style.BackColor = Color.White;

            }
            else
            {
                dataGridView1.SelectedCells[i].Style.BackColor = Color.Yellow;

            }
           dataGridView1.CurrentCell = null;
        }
    }

    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        byte _selected = 0;

        for (int i = 0; i < dataGridView1.SelectedCells.Count; i++)
        {
            counter(_selected);
        }            
    }

    public void counter(int count)
    {
        count++;enter code here
        MessageBox.Show(count.ToString());
    }

以下是表单的外观。 form

游戏的名称是基诺,我尝试创建它。也许我有些错误,抱歉。

2 个答案:

答案 0 :(得分:0)

您可以使用此链接计算单元格,但您可以在此游戏中使用其他组件或WPF。

https://msdn.microsoft.com/en-us/library/x8x9zk5a(v=vs.85).aspx

答案 1 :(得分:0)

这是我的代码旋转。在这个片段中,我使用int yellowed来跟踪有多少个单元格是黄色的。当单元格上的用户clicks时,单元格计数器设置黄色计数。当鼠标按钮向上(dataGridView1_CellMouseUp)时,只允许相应数量的单元格变黄。

using System.Drawing;
using System.Windows.Forms;

namespace DataGridView_47478857
{
    public partial class Form1 : Form
    {

        DataGridView dataGridView1 = new DataGridView();
        int yellowed = 0;
        int maxYellowed = 15;
        public Form1()
        {
            InitializeComponent();
            dataGridView1.Dock = DockStyle.Fill;
            dataGridView1.CellMouseUp += dataGridView1_CellMouseUp;
            dataGridView1.CellClick += dataGridView1_CellClick;
            this.Controls.Add(dataGridView1);


            dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
            //выделение только ячеек

            // создаём массив

            int[,] Array = new int[8, 10];

            byte numbers = 1;

            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    Array[i, j] = numbers;
                    numbers++;
                }
            }

            dataGridView1.RowCount = 8;
            dataGridView1.ColumnCount = 10;

            // программно записываем массив и задаём стиль ячеек

            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    dataGridView1.Columns[j].Width = 30;
                    dataGridView1.Rows[i].Height = 30;
                    dataGridView1.Rows[i].Cells[j].Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                    dataGridView1.Rows[i].Cells[j].Style.Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold);
                    dataGridView1.Rows[i].Cells[j].Value = Array[i, j].ToString();
                }
            }
        }

        private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e) // выделение ячеек
        {

            for (int i = 0; i < dataGridView1.SelectedCells.Count; i++)
            {
                if (dataGridView1.SelectedCells[i].Style.BackColor == Color.Yellow)
                {
                    dataGridView1.SelectedCells[i].Style.BackColor = Color.White;

                }
                else
                {
                    if (yellowed < maxYellowed)//only color code this cell if the yellow cell count has not been exceeded
                    {
                        dataGridView1.SelectedCells[i].Style.BackColor = Color.Yellow;
                        yellowed++;
                    }
                }
            }
            dataGridView1.ClearSelection();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            yellowed = 0;
            foreach (DataGridViewRow currentRow in dataGridView1.Rows)
            {
                foreach (DataGridViewCell currentCell in currentRow.Cells)
                {
                    if (currentCell.Style.BackColor == Color.Yellow)
                    {
                        yellowed++;
                    }
                }
            }
        }

    }
}