728x90
1. 리스트뷰 OwnerDraw = true로 변경 (View는 Details)
2. 전체 소스코드
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 myPractice
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
drawListView();
}
private void drawListView()
{
listView1.CheckBoxes = true;
ListViewItem lvi1 = new ListViewItem(); lvi1.SubItems.Add("aaa"); lvi1.SubItems.Add("bbb");
ListViewItem lvi2 = new ListViewItem(); lvi2.SubItems.Add("ccc"); lvi2.SubItems.Add("ddd");
ListViewItem lvi3 = new ListViewItem(); lvi3.SubItems.Add("eee"); lvi3.SubItems.Add("fff");
listView1.Items.Add(lvi1);
listView1.Items.Add(lvi2);
listView1.Items.Add(lvi3);
ColumnHeader columnHeader_check = new System.Windows.Forms.ColumnHeader();
ColumnHeader columnHeader_1 = new System.Windows.Forms.ColumnHeader();
ColumnHeader columnHeader_2 = new System.Windows.Forms.ColumnHeader();
columnHeader_1.Text = "항목1";
columnHeader_1.Width = 200;
columnHeader_2.Text = "항목2";
columnHeader_2.Width = 200;
listView1.DrawColumnHeader += new DrawListViewColumnHeaderEventHandler(drawListColumnHeader);
listView1.DrawItem += new DrawListViewItemEventHandler(lv_DrawItem);
listView1.DrawSubItem += new DrawListViewSubItemEventHandler(lv_DrawSubItem);
listView1.ColumnClick += new ColumnClickEventHandler(lv_ColumnClick);
listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
columnHeader_check, columnHeader_1, columnHeader_2});
}
private void drawListColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
if (e.ColumnIndex == 0)
{
e.DrawBackground();
bool value = false;
try
{
value = Convert.ToBoolean(e.Header.Tag);
}
catch (Exception)
{
}
CheckBoxRenderer.DrawCheckBox(e.Graphics, new Point(e.Bounds.Left + 4, e.Bounds.Top + 4),
value ? System.Windows.Forms.VisualStyles.CheckBoxState.CheckedNormal :
System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal);
}
else e.DrawDefault = true;
}
private void lv_ColumnClick(object sender, ColumnClickEventArgs e)
{
if (e.Column == 0)
{
bool value = false;
try
{
value = Convert.ToBoolean(this.listView1.Columns[e.Column].Tag);
}
catch (Exception)
{
}
this.listView1.Columns[e.Column].Tag = !value;
foreach (ListViewItem item in this.listView1.Items) item.Checked = !value;
this.listView1.Invalidate();
}
}
private void lv_DrawItem(object sender, DrawListViewItemEventArgs e)
{
e.DrawDefault = true;
}
private void lv_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
e.DrawDefault = true;
}
}
}