Last active
June 26, 2020 11:47
-
-
Save jayu108/f1f963641593ad59a4f92be7bd2c3298 to your computer and use it in GitHub Desktop.
datagridview 에 checkbox 표시하기, 일련번호 , 마우스로 column sizw 조절 못하게 하기.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 datagridview_checkbox | |
{ | |
public partial class Form1 : Form | |
{ | |
private List<Stock> _stockList = new List<Stock>(); | |
private DataTable _dt = new DataTable(); // original data | |
public Form1() | |
{ | |
InitializeComponent(); | |
this._stockList.Add(new Stock() { StockCode = "005930", StockName = "삼성전자", Price = 44750 }); | |
this._stockList.Add(new Stock() { StockCode = "068270", StockName = "셀트리온", Price = 213500 }); | |
this._stockList.Add(new Stock() { StockCode = "215600", StockName = "신라젠", Price = 64100 }); | |
this._stockList.Add(new Stock() { StockCode = "003490", StockName = "대한항공", Price = 34350 }); | |
this._stockList.Add(new Stock() { StockCode = "011150", StockName = "CJ씨푸드", Price = 3130 }); | |
} | |
private void Form1_Load(object sender, EventArgs e) | |
{ | |
this.makeDataTable(); | |
this.dataGridView1.DataSource = this._dt; | |
this.dataGridView1.AllowUserToAddRows = false; | |
this.dataGridView1.Columns[0].Width = 20; | |
this.dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing; | |
this.dataGridView1.Columns[0].Resizable = DataGridViewTriState.False; | |
this.dataGridView1.Columns[1].ReadOnly = true; | |
this.dataGridView1.Columns[2].ReadOnly = true; | |
this.dataGridView1.Columns[3].ReadOnly = true; | |
} | |
private void makeDataTable() | |
{ | |
_dt.Columns.Add(" ", typeof(bool)); // 선택 체크박스 용 | |
_dt.Columns.Add("종목코드", typeof(string)); | |
_dt.Columns.Add("종목명", typeof(string)); | |
_dt.Columns.Add("가격", typeof(int)); | |
foreach (var stock in _stockList) | |
{ | |
_dt.Rows.Add(false, stock.StockCode, stock.StockName, stock.Price); | |
} | |
} | |
// row header 에 자동 일련번호 넣기 | |
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) | |
{ | |
StringFormat drawFormat = new StringFormat(); | |
//drawFormat.FormatFlags = StringFormatFlags.DirectionVertical; | |
drawFormat.FormatFlags = StringFormatFlags.DirectionRightToLeft; | |
using (Brush brush = new SolidBrush(Color.Red)) | |
{ | |
e.Graphics.DrawString((e.RowIndex + 1).ToString(), e.InheritedRowStyle.Font, | |
brush, e.RowBounds.Location.X + 35, e.RowBounds.Location.Y + 4, drawFormat); | |
} | |
} | |
// checkBox 변경 상태를 확실히 반영하기 위함. | |
private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e) | |
{ | |
if (dataGridView1.CurrentCell is DataGridViewCheckBoxCell) | |
{ | |
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit); | |
} | |
} | |
private void button1_Click(object sender, EventArgs e) | |
{ | |
foreach(DataRow dr in _dt.Rows) | |
{ | |
Console.WriteLine($"checkbox 상태 = {dr[0]}"); | |
} | |
} | |
} | |
public class Stock | |
{ | |
public string StockCode { get; set; } | |
public string StockName { get; set; } | |
public int Price { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment