簡體   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