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;
}
}
}
컬럼헤더를 직접 만들어야합니다.
컬럼헤더 만들때, 맨 앞 컬럼헤더면 체크박스를 추가해야하기때문에
컬럼헤더 만드는 listView.DrawColumnHeader함수를 직접 만든 함수로 추가해야합니다
마찬가지로 컬럼헤더 클릭할 때도, 맨 앞 컬럼이면 전체 체크박스 상태를 설정해야하므로
ColumnClick 함수도 추가해야합죠
listview.CheckBoxes = true;로 해주는것도 잊지마세용