본문 바로가기

C#

[질답] C#의 DataGridView에서 행이 추가 될 때의 이벤트와 행을 추가시키는 이벤트

반응형
질문

C#의 DataGridView에서 행이 추가 될 때의 이벤트와 행을 추가시키는 이벤트 내공100

비공개
질문186건
질문마감률99.3%
질문채택률88.1%
2018.01.04. 18:05
조회수15
안녕하세요. 독학 공부 중입니다.


C#의 DataGridView에서 행이 추가 될 때의 이벤트와 행을 추가시키는 이벤트를 찾고 있는데요.
아직 MSDN이 익숙하지 않아서 제가 필요한 정보를 찾는게 너무 어렵네요...


1. DataGridView에서 행이 추가 될 때 ComboBox의 아이템을 추가하는 코딩을 해야하는데,
해당 이벤트의 이름이 뭔지를 찾을 수가 없네요 ㅠㅠ.. 아래의 코드를 입력하려고 합니다.
소스코드 원문보기
  1.       DataGridViewComboBoxCell comboBoxCell = new DataGridViewComboBoxCell(); 
  2.       comboBoxCell.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox; 
  3.       comboBoxCell.Items.Add("CASH"); 
  4.       comboBoxCell.Items.Add("CARD"); 
  5.       DGV_Sales.Rows[i].Cells[2] = comboBoxCell; 

2. DataGridView를 쓸때, 어떤 셀이든 값이 입력 되면 바로 아래에 행이 추가 되는데,
이 부분을 마지막 행의 입력이 끝나면 한줄 더 생기는 이벤트로 변경하려고 하는데 방법을 모르겠네요..


도움 부탁 드립니다!!



위의 질문이 있어서 시간이 좀 있었기에 샘플을 좀 만들어봤습니다.


우선은, 그리드뷰에서 행추가 이벤트를 삭제합니다. (일본어라 죄송합니다;)

그리고 코드에서는 아래와같이 짜봤습니다.

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
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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        DataGridViewComboBoxEditingControl dataGridViewComboBox = null;
        public Form1()
        {
            InitializeComponent();
            dataGridView1.Rows.Add();
        }
        private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            DataGridViewComboBoxCell comboBoxCell = new DataGridViewComboBoxCell();
            comboBoxCell.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;
            comboBoxCell.Items.Add("CASH");
            comboBoxCell.Items.Add("CARD");
            dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[2= comboBoxCell;
        }
        private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (e.Control is DataGridViewComboBoxEditingControl &&
                ((System.Windows.Forms.DataGridViewComboBoxEditingControl)e.Control).EditingControlRowIndex ==
                    dataGridView1.Rows.Count - 1)
            {
                DataGridView dgv = (DataGridView)sender;
                this.dataGridViewComboBox = (DataGridViewComboBoxEditingControl)e.Control;
                this.dataGridViewComboBox.SelectedIndexChanged += new EventHandler(dataGridViewComboBox_SelectedIndexChanged);
            }
        }
        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dataGridViewComboBox != null)
            {
                this.dataGridViewComboBox.SelectedIndexChanged -= new EventHandler(dataGridViewComboBox_SelectedIndexChanged);
                this.dataGridViewComboBox = null;
            }
        }
        private void dataGridViewComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((System.Windows.Forms.ComboBox)sender).Text))
            {
                dataGridView1.Rows.Add();
            }
        }
    }
}
 
cs


위의 소스에 대한 샘플은 아래의 첨부파일로 확인하실수 있습니다.

VS 2015 로 만들어졌습니다.

naver-0105.zip


반응형