繁体   English   中英

datagridview计数单元格c#

[英]datagridview count cells c#

我有个问题。 我需要计算激活的细胞,它们是黄色的,但我不知道该怎么做。 在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());
    }

这是表格的外观。 形成

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

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

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

这是我对您的代码的看法。 在此代码段中,我将使用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++;
                    }
                }
            }
        }

    }
}

暂无
暂无

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

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