實作類別似AutoCAD或者PhotoShop中圖層的效果。層名稱,層狀態,鎖定,顯示。
總結一下DataGridView的小技巧:1.代碼添加列,次序是由右至左,即越先添加的列,越靠右。2.代碼添加行,先添加行數,再往行裡加資料。 dataGridView1.Rows.Add(3); dataGridView1.Rows[0].Cells[0].Value = "圖層一";3.圖片列的使用DataGridViewImageColumn4.去掉列名 dataGridView1.RowHeadersVisible = false;5.ListView也可以實現同樣的效果,但是介面很不好控制。 ListBox可以編輯但是無法添加映像列。 C# WinForm控制項ListBox點選可編輯(附源碼)
代碼: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 WinDataGridView
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void BindData()
{
//去掉列名
dataGridView1.RowHeadersVisible = false;
//圖片列,列的添加是由右至左
//是否鎖定
DataGridViewImageColumn colLock = new DataGridViewImageColumn();
colLock.Name = "layerLock";
colLock.HeaderText = "鎖";
colLock.Width = 20;
dataGridView1.Columns.Insert(0, colLock);
//是否顯示
DataGridViewImageColumn colShow = new DataGridViewImageColumn();
colShow.Name = "layerShow";
colShow.HeaderText = "顯";
colShow.Width = 20;
dataGridView1.Columns.Insert(0, colShow);
//編輯框列
DataGridViewTextBoxColumn colTxt = new DataGridViewTextBoxColumn();
colTxt.Name = "layerName";
colTxt.HeaderText = "圖層名稱";
dataGridView1.Columns.Insert(0, colTxt);
//添加行資料
dataGridView1.Rows.Add(3);
this.dataGridView1.Rows[0].Cells[0].Value = "圖層一";
this.dataGridView1.Rows[0].Cells[1].Value = imageList1.Images[0];
this.dataGridView1.Rows[0].Cells[2].Value = imageList1.Images[2];
this.dataGridView1.Rows[1].Cells[0].Value = "圖層二";
this.dataGridView1.Rows[1].Cells[1].Value = imageList1.Images[0];
this.dataGridView1.Rows[1].Cells[2].Value = imageList1.Images[3];
this.dataGridView1.Rows[2].Cells[0].Value = "圖層三";
this.dataGridView1.Rows[2].Cells[1].Value = imageList1.Images[1];
this.dataGridView1.Rows[2].Cells[2].Value = imageList1.Images[2];
this.dataGridView1.Rows[3].Cells[0].Value = "圖層四";
this.dataGridView1.Rows[3].Cells[1].Value = imageList1.Images[1];
this.dataGridView1.Rows[3].Cells[2].Value = imageList1.Images[3];
}
private void Form1_Load(object sender, EventArgs e)
{
BindData();
}
}
}源碼:http://files.cnblogs.com/greatverve/WinDataGridView.rar