C# 在C中更改网格单元的颜色#

C# 在C中更改网格单元的颜色#,c#,colors,grid,C#,Colors,Grid,我正在用C#编写一个占用率网格映射程序。我想更改网格单元的颜色 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Imaging; using System.Linq; using System.Text; using System.Threading.Tasks; us

我正在用C#编写一个占用率网格映射程序。我想更改网格单元的颜色

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Controls;
using System.Windows;


namespace gridappl
{
    public partial class Form1 : Form
    {
        private Window mainWindow;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {


            // Create the application's main window

            mainWindow = new Window();
            mainWindow.Title = "Grid Sample";
            // Create the Grid
            Grid myGrid = new Grid();
            myGrid.Width = 600;
            myGrid.Height = 600;
            //myGrid.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            //myGrid.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            myGrid.ShowGridLines = true;


            // Define the Columns
            ColumnDefinition colDef1 = new ColumnDefinition();
            ColumnDefinition colDef2 = new ColumnDefinition();
            ColumnDefinition colDef3 = new ColumnDefinition();

            myGrid.ColumnDefinitions.Add(colDef1);
            myGrid.ColumnDefinitions.Add(colDef2);
            myGrid.ColumnDefinitions.Add(colDef3);


            // Define the Rows
            RowDefinition rowDef1 = new RowDefinition();
            RowDefinition rowDef2 = new RowDefinition();
            RowDefinition rowDef3 = new RowDefinition();

            myGrid.RowDefinitions.Add(rowDef1);
            myGrid.RowDefinitions.Add(rowDef2);
            myGrid.RowDefinitions.Add(rowDef3);



            // Add the Grid as the Content of the Parent Window Object
            mainWindow.Content = myGrid;
            mainWindow.Show();


        }
    }
}
我尝试使用许多命令,如以下命令:

myGrid.Background = Brushes.Azure;

myGrid.Background = System.Drawing.Brushes.Red;
但是,程序仍然会抛出错误。根据我们的选择,应该如何更改网格单元的颜色?

您可以尝试以下代码:

for (int i = 0; i < myGrid.Rows.Count; i++)
            {
                myGrid.Rows[i].Cells[/*Cell you want the color*/].Style.BackColor = /*color you want*/
            }
例如:

myGrid.Rows[0].Cells[1].Style.BackColor = Color.Red;
您可以尝试以下代码:

for (int i = 0; i < myGrid.Rows.Count; i++)
            {
                myGrid.Rows[i].Cells[/*Cell you want the color*/].Style.BackColor = /*color you want*/
            }
例如:

myGrid.Rows[0].Cells[1].Style.BackColor = Color.Red;


我添加了这样一行:“myGrid.Rows[4].Cells[5].Style.BackColor=Azure;”但程序仍然会抛出错误,因为您仍然需要将其设置为myGrid.Rows[4].Cells[5].Style.BackColor=Color.Azure或
=Color.FromArgb(51、102、153)我更喜欢使用它,因为它可以调整颜色的明暗程度。现在错误显示在
行中。错误消息如下所示:“网格不包含“Rows”的定义,并且找不到接受“Grid”类型的第一个参数的扩展方法“Rows”。您只添加了3行定义和3列定义。那你怎么设置第四排的背景色呢?myGrid.Rows[4]。单元格[5]。Style.BackColor=Azure;给出
单元格[5]
是否正确,因为您提到我们需要输入要着色的单元格的编号。我添加了一行如下内容:“myGrid.Rows[4].Cells[5].Style.BackColor=Azure;”但程序仍然会抛出错误,因为您仍然需要这样设置
myGrid.Rows[4].Cells[5].Style.BackColor=Color.Azure
=Color.FromArgb(51102153)我更喜欢使用它,因为它可以调整颜色的明暗程度。现在错误显示在
行中。错误消息如下所示:“网格不包含“Rows”的定义,并且找不到接受“Grid”类型的第一个参数的扩展方法“Rows”。您只添加了3行定义和3列定义。那你怎么设置第四排的背景色呢?myGrid.Rows[4]。单元格[5]。Style.BackColor=Azure;给出
单元格[5]
是否正确,因为您提到我们需要输入要着色的单元格的编号。请尝试第二行代码填充所有行和列单元格的背景色。第二行代码可以很好地使网格中的所有单元格变为绿色。如何给网格中的特定单元格着色?能否将代码粘贴到网格中添加UI元素的位置?所以,我可以在此基础上提供解决方案。到目前为止,我还没有创建任何UI元素。我想在代码本身中指定特定的单元格必须着色。在网格包中没有为特定单元格定义背景的方法。但是,您可以将背景添加到将显示在网格上方的UIelement中。因此,您可以实现预期的输出。请尝试使用第二行代码填充所有行和列单元格的背景色。第二行代码可以很好地将网格中的所有单元格变成绿色。如何给网格中的特定单元格着色?能否将代码粘贴到网格中添加UI元素的位置?所以,我可以在此基础上提供解决方案。到目前为止,我还没有创建任何UI元素。我想在代码本身中指定特定的单元格必须着色。在网格包中没有为特定单元格定义背景的方法。但是,您可以将背景添加到将显示在网格上方的UIelement中。因此,您可以实现预期的输出。
myGrid.Background= new SolidColorBrush(Colors.Green);
foreach (DataGridViewRow row in dgv_List.Rows)
{
  if (intcolumncunt == 2)
  {
    if(Convert.ToInt32(row.Cells[2].Value.ToString()) > Convert.ToInt32(row.Cells[3].Value.ToString()))
     {
      row.DefaultCellStyle.BackColor = Color.Red; 
     }
  }
}