Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DataGridView,循环所有行,如果存在,则进行乘法运算_C#_Gridview_Datagrid - Fatal编程技术网

C# DataGridView,循环所有行,如果存在,则进行乘法运算

C# DataGridView,循环所有行,如果存在,则进行乘法运算,c#,gridview,datagrid,C#,Gridview,Datagrid,嗨,我需要一些帮助 我正在做一种“生命游戏”游戏。无论如何,我想使用网格视图检查当前单元格的邻居是否与当前单元格的值相同。如果是,我想检查(当前单元格周围)是否有空白,并添加邻居的值,但要使用和额外的字符 这是我的密码: public void check() { Textual txt = new Textual(); for (int x = 0; x < dataGridView1.Rows.Count; x++) { for (int y = 0; y <

嗨,我需要一些帮助

我正在做一种“生命游戏”游戏。无论如何,我想使用网格视图检查当前单元格的邻居是否与当前单元格的值相同。如果是,我想检查(当前单元格周围)是否有空白,并添加邻居的值,但要使用和额外的字符

这是我的密码:

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;
            }
          }
        }
      }
    }
  }
公共作废检查()
{
text txt=新的text();
对于(int x=0;x=0&&
y+1=0&&x+1
这是一个可以用来查找匹配邻居的函数。如果您选择二维数组而不是网格,这仍然是非常相似的,这将是最好的解决方案,因为控件应该用于与人的交互,并且数据应该被操纵,而不管呈现如何

要查找给定单元格的匹配值(注意x和y-您切换了它们的通常含义):

要查找空单元格,请执行以下操作:

cell = FindCell(new Point(x, y), string.Empty);
编辑:更好的用法示例

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:我假设从您的代码中,您的单元格将具有非空值。由于情况并非如此,请将每个cell.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;
}
//
///给定位置周围的位置列表。添加到上一个值以获取下一个位置。
/// 
点[]邻居=新点[]
{
新点(-1,-1),
新点(1,0),
新点(1,0),
新点(-2,1),
新点(2,0),
新点(-2,1),
新点(1,0),
新点(1,0),
};
/// 
///查找包含给定字符串值的单元格。
/
/// <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;
}