Ячейки dataGridView разного цвета - C#
Формулировка задачи:
Ребята помогите сделать так
Вот уже есть наброски кода. Подскажите как это сделать? Что нужно исправить и дописать.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication25
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int n, a, b;
n = Convert.ToInt32(textBox1.Text);
a = n;
b = n;
int[,] mas = new int[a, b];
dataGridView1.RowCount = a; // количество строк
dataGridView1.ColumnCount = b; // количество столбцов
for (int i = 0; i < b; i++)
{
dataGridView1.Columns[i].Width = 30; // указываем ширину столбика
}
for (int i = 0; i < a; i++)
{
dataGridView1.Rows[a-1].Cells[i].Style.BackColor = Color.Red;
dataGridView1.Rows[i].Cells[b-1].Style.BackColor = Color.Red;
for (int j = 0; j < b; j++)
{
if (i == mas[0, 0 + 1] || j == mas[0 +1, 0])
{
dataGridView1.Rows[i].Cells[j].Style.BackColor = Color.Red;
}
}
}
}
}
}Решение задачи: «Ячейки dataGridView разного цвета»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication25
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int n, a, b;
n = Convert.ToInt32(textBox1.Text);
a = n;
b = n;
int[,] mas = new int[a, b];
dataGridView1.RowCount = a; // количество строк
dataGridView1.ColumnCount = b; // количество столбцов
for (int i = 0; i < b; i++)
{
dataGridView1.Columns[i].Width = 30; // указываем ширину столбика
}
for (int t = 1; t < n; t++)
{
for (int i = 0; i < a; i++)
{
for (int j = 0; j < b; j++)
{
dataGridView1.Rows[a-1].Cells[j].Style.BackColor = Color.Blue;
dataGridView1.Rows[i].Cells[b-1].Style.BackColor = Color.Blue;
if (i == mas[0 + 1, 0] || j == mas[0, 0 + 1])
{
dataGridView1.Rows[i].Cells[j].Style.BackColor = Color.Blue;
}
}
}
}
for (int t = 1; t < n; t++)
{
for (int i = 1; i < a-t; i++)
{
for (int j = 1; j < b-t; j++)
{
dataGridView1.Rows[a - 2].Cells[j].Style.BackColor = Color.Red;
dataGridView1.Rows[i].Cells[b - 2].Style.BackColor = Color.Red;
}
}
}
for (int t = 1; t < n; t++)
{
for (int i = 1; i < a - t; i++)
{
for (int j = 1; j < b - t; j++)
{
dataGridView1.Rows[a - 4].Cells[j].Style.BackColor = Color.Red;
dataGridView1.Rows[i].Cells[b - 4].Style.BackColor = Color.Red;
}
}
}
}
}
}