DataGridView,遍历所有行并相乘(如果存在)

本文关键字:如果 存在 遍历 DataGridView | 更新日期: 2023-09-27 18:27:26

嗨,我需要一些帮助,

我正在制作一款"人生游戏"。无论如何,我想使用网格视图检查当前单元格的邻居是否与我当前单元格的值相同。如果是,我想检查(当前单元格周围)是否有空格,并添加邻居的值,但要添加一个额外的字符。

这是我的代码:

public void check()
{
  Textual txt = new Textual();
  for (int x = 0; x < dataGridView1.Rows.Count; x++)
  {
    for (int y = 0; y < dataGridView1.Rows[x].Cells.Count; y++)
    {
        if (dataGridView1.Rows[x].Cells[y].Value == "VS" ||
            dataGridView1.Rows[x].Cells[y].Value == "SL" ||
            dataGridView1.Rows[x].Cells[y].Value == "KG" ||
            dataGridView1.Rows[x].Cells[y].Value == "KN")
        {
          if (y - 1 >= 0 &&
              y + 1 < dataGridView1.ColumnCount &&
              x - 1 >= 0 && x + 1 < dataGridView1.RowCount &&
              y + 1 < dataGridView1.ColumnCount &&
              x + 1 < dataGridView1.RowCount)
          {
            string value = dataGridView1.Rows[x].Cells[y].Value.ToString();
            int j;
            int k;
            switch (value)
            {
              case "VS":
              if (dataGridView1.Rows[x].Cells[y + 1].Value == "")
              {
                dataGridView1.Rows[x].Cells[y + 1].Value = "VS++";
                j = x;
                k = y + 1;
                int[,] temp= new int[j, k];
                or.oznaka = "VS";
                popis.TryAdd(temp, or);
                dataGridView1.Rows[x].Cells[y + 1].Style.BackColor = System.Drawing.Color.Red;
              }
              else if (dataGridView1.Rows[x - 1].Cells[y].Value == "")
              {
                dataGridView1.Rows[x - 1].Cells[y].Value = "VS++";
                j = x - 1;
                k = y;
                int[,] temp= new int[j, k];
                or.oznaka = "VS";
                popis.TryAdd(temp, or);
                dataGridView1.Rows[x - 1].Cells[y].Style.BackColor = System.Drawing.Color.Yellow;
              }
              else if (dataGridView1.Rows[x + 1].Cells[y].Value == "")
              {
                dataGridView1.Rows[x + 1].Cells[y].Value = "VS++";
                j = x + 1;
                k = y;
                int[,] temp= new int[j, k];
                or.oznaka = "VS";
                popis.TryAdd(temp, or);
                dataGridView1.Rows[x + 1].Cells[y].Style.BackColor = System.Drawing.Color.Gold;
              }
              else if (dataGridView1.Rows[x + 1].Cells[y + 1].Value == "")
              {
                dataGridView1.Rows[x + 1].Cells[y + 1].Value = "VS+";
                j = x - 1;
                k = y + 1;
                int[,] temp= new int[j, k];
                or.oznaka = "VS";
                popis.TryAdd(temp, or);
                dataGridView1.Rows[x + 1].Cells[y + 1].Style.BackColor = System.Drawing.Color.Green;
              }
              else if (dataGridView1.Rows[x - 1].Cells[y - 1].Value == "")
              {
                dataGridView1.Rows[x - 1].Cells[y - 1].Value = "VS+";
                j = x - 1;
                k = y - 1;
                int[,] temp= new int[j, k];
                or.oznaka = "VS";
                popis.TryAdd(temp,or);
                dataGridView1.Rows[x - 1].Cells[y - 1].Style.BackColor = System.Drawing.Color.Purple;
              }
              else if (dataGridView1.Rows[x - 1].Cells[y + 1].Value == "")
              {
                dataGridView1.Rows[x - 1].Cells[y + 1].Value = "VS+";
                j = x - 1;
                k = y + 1;
                int[,] temp= new int[j, k];
                or.oznaka = "VS";
                popis.TryAdd(temp, or);
                dataGridView1.Rows[x - 1].Cells[y + 1].Style.BackColor = System.Drawing.Color.HotPink;
              }
              else if (dataGridView1.Rows[x].Cells[y - 1].Value == "")
              {
                dataGridView1.Rows[x].Cells[y - 1].Value = "VS+";
                j = x;
                k = y - 1;
                int[,] temp= new int[j, k];
                or.oznaka = "VS";
                popis.TryAdd(temp, or);
                dataGridView1.Rows[x].Cells[y - 1].Style.BackColor = System.Drawing.Color.Lavender;
              }
              else if (dataGridView1.Rows[x + 1].Cells[y - 1].Value == "")
              {
                dataGridView1.Rows[x + 1].Cells[y - 1].Value = "VS+";
                j = x + 1;
                k = y - 1;
                int[,] temp= new int[j, k];
                or.oznaka = "VS";
                popis.TryAdd(temp, or);
                dataGridView1.Rows[x + 1].Cells[y - 1].Style.BackColor = System.Drawing.Color.LightBlue;
              }
              break;
            }
          }
        }
      }
    }
  }

DataGridView,遍历所有行并相乘(如果存在)

这里有一个可以用来查找匹配邻居的函数。如果你选择二维数组而不是网格,这将是最好的解决方案,因为控件应该用于与人类的交互,数据应该被操纵,而不管呈现如何。

要找到给定单元格的匹配值(记住x和y-您切换了它们的常用含义):

cell = FindCell(new Point(x, y), dataGridView1.Rows[y].Cells[x]);

查找空单元格:

cell = FindCell(new Point(x, y), string.Empty);

EDIT:更好的使用示例

foreach (DataGridViewRow row in dataGridView1.Rows)
{
    foreach (DataGridViewCell cell in row.Cells)
    {
        if (cell.Value != null && cell.Value.ToString() != string.Empty)
        {
            DataGridViewCell neighbour = FindCell(new Point(cell.ColumnIndex, row.Index), cell.Value.ToString());
            //  Found
            if (neighbour != null)
            {
                DataGridViewCell emptyCell = FindCell(new Point(cell.ColumnIndex, row.Index), string.Empty);
                if (emptyCell != null)
                {
                    emptyCell.Value = "WHATEVERYOUREQUIREITTOBE";
                }
            }
        }
    }
}

编辑2:我从你的代码中假设你的单元格将有一个非空值。事实并非如此,请更换每个单元格。Value.ToString()与(cell.Value??string.Empty).ToString(),就像我在示例中所做的那样。

在之后测试null以确保找到单元格。以下是功能:

/// <summary>
/// List of locations around given location. Add to previous value to get next location.
/// </summary>
Point[] neighbours = new Point[]
{
    new Point (-1, -1),
    new Point (1, 0),
    new Point (1, 0),
    new Point (-2, 1),
    new Point (2, 0),
    new Point (-2, 1),
    new Point (1, 0),
    new Point (1, 0),
};
/// <summary>
/// Finds a cell containing given string value.
/// </summary>
/// <param name="location">Point of search</param>
/// <param name="value">Value to find</param>
/// <returns>Cell containing given value</returns>
DataGridViewCell FindCell(Point location, string value)
{
    for (int i = 0; i < neighbours.Length; ++i)
    {
        //  Move location to new point
        location.Offset(neighbours[i]);
        //  Check boundaries
        if (location.Y >= 0 && location.Y < dataGridView1.RowCount
            && location.X >= 0 && location.X < dataGridView1.Columns.Count)
        {
            //  Get cell 
            DataGridViewCell cell = dataGridView1.Rows[location.Y].Cells[location.X];
            //  If value matches
            if ((cell.Value ?? string.Empty).ToString() == value)
            {
                return cell;
            }
        }
    }
    //  No match
    return null;
}