■ DataGridView 클래스에서 복수 선택 행을 삭제하는 방법을 보여준다.
▶ MainForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
namespace TestProject; /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; this.dataGridView.ColumnCount = 4; this.dataGridView.Columns[0].Name = "도서명"; this.dataGridView.Columns[1].Name = "대출일"; this.dataGridView.Columns[2].Name = "반환일"; this.dataGridView.Columns[3].Name = "기간"; this.dataGridView.Rows.Add("도서 1", "2018/01/13", "2018/01/20"); this.dataGridView.Rows.Add("도서 2", "2018/03/05", "2018/03/13"); this.dataGridView.Rows.Add("도서 3", "2018/04/20", "2018/05/15"); this.dataGridView.Rows.Add("도서 4", "2018/04/18", "2018/05/11"); this.dataGridView.Rows.Add("도서 5", "2018/06/01", "2018/08/22"); this.dataGridView.Rows.Add("도서 6", "2018/05/30", "2018/06/02"); this.dataGridView.Rows.Add("도서 7", "2018/04/04", "2018/05/12"); this.dataGridView.Rows.Add("도서 8", "2018/03/12", "2018/04/10"); this.deleteButton.Click += deleteButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 삭제 버튼 클릭시 처리하기 - deleteButton_Click(sender, e) /// <summary> /// 삭제 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void deleteButton_Click(object sender, EventArgs e) { foreach(DataGridViewRow row in this.dataGridView.SelectedRows) { this.dataGridView.Rows.Remove(row); } } #endregion } |