110,539
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.AllowUserToAddRows = false;
var c1 = new DataGridViewTextBoxColumn();
c1.HeaderText = "属性";
var c2 = new DataGridViewImageColumn();
c2.HeaderText = "颜色";
dataGridView1.Columns.Add(c1);
dataGridView1.Columns.Add(c2);
var row1 = new DataGridViewRow();
row1.Cells.Add(new DataGridViewTextBoxCell());
row1.Cells.Add(new DataGridViewImageCell());
row1.Cells[0].Value = "Red";
row1.Cells[1].Value = CreateImage(Color.Red);
dataGridView1.Rows.Add(row1);
var row2 = new DataGridViewRow();
row2.Cells.Add(new DataGridViewTextBoxCell());
row2.Cells.Add(new DataGridViewImageCell());
row2.Cells[0].Value = "Blue";
row2.Cells[1].Value = CreateImage(Color.Blue);
dataGridView1.Rows.Add(row2);
}
private Image CreateImage(Color color)
{
Bitmap bmp = new Bitmap(100, 20);
var g = Graphics.FromImage(bmp);
g.FillRectangle(new SolidBrush(color), 0, 0, bmp.Width, bmp.Height);
return bmp;
}
}
}
在窗体上放一个DataGridView,双击窗体,在Form_Load中添加上述代码。
运行