안녕하세요.
오늘은 C# DevExpress에서 제공하는 컨트롤인 GridControl(그리드컨트롤)을 이용하여 그리드컨트롤에서 선택된 셀의 데이터를 가져오는 방법에 대해서 알려드리려고 합니다.
즉, 그리드에 데이터들이 있는데, 여기서 사용자가 특정 컬럼의 셀을 선택 했을 시 그 데이터 값을 가져온다는 얘기 입니다.
그럼 바로 예제 코드를 통해서 알아보도록 하겠습니다.
먼저, 빈 윈폼 프로젝트를 생성해 주시고 아래와 같이 GridControl을 배치해 주시기 바랍니다.
윈폼 프로젝트 생성 및 GridControl 배치
위와 같이 빈 폼에 GridControl을 배치해 주시기 바랍니다.
그럼 이제 예제 코드를 작성해 보도록 하겠습니다.
예제 코드
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
using DevExpress.Utils; using DevExpress.XtraEditors.Repository; using DevExpress.XtraGrid.Views.Grid; 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 DevTest { public partial class Form1 : DevExpress.XtraEditors.XtraForm { public Form1() { InitializeComponent();
//폼 Shown 이벤트 선언 this.Shown += DevForm_Shown; }
/// <summary> /// 폼이 보여질 때, 일어나는 이벤트 핸들러 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void DevForm_Shown(object sender, EventArgs e) { //GridControl 초기화 InitGridControl();
//DataTable 데이터 저장 및 바인딩 this.uiGrid_Main.DataSource = GetData();
gridView1.Columns[0].View.OptionsBehavior.EditorShowMode = EditorShowMode.MouseUp; gridView1.RowCellClick += RowCellClick_Event; }
/// <summary> /// GridView 초기화 /// </summary> public void InitGridControl() { GridView gv = this.uiGrid_Main.MainView as GridView; gv.OptionsView.ShowGroupPanel = false; gv.OptionsBehavior.Editable = true; }
public DataTable GetData() { DataTable dt = new DataTable(); dt.Columns.Add("Name"); dt.Columns.Add("Age"); dt.Columns.Add("Grade"); dt.Columns.Add("Score"); dt.Columns.Add("Check");
dt.Rows.Add(new string[] { "범범조조", "28", "2", "100", "True" }); dt.Rows.Add(new string[] { "황선홍", "56", "3", "10", "True" }); dt.Rows.Add(new string[] { "안정환", "34", "2", "78", "False" }); dt.Rows.Add(new string[] { "설기현", "33", "1", "54", "False" }); dt.Rows.Add(new string[] { "아이유", "28", "1", "45", "True" }); dt.Rows.Add(new string[] { "박지성", "23", "4", "10", "False" });
return dt; }
/// <summary> /// RowCell 클릭 이벤트 핸들러 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void RowCellClick_Event(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e) { if (e.Column.FieldName.Equals("Name")) { //그리드의 선택된 cell의 데이터 가져오기 string str = gridView1.GetFocusedRowCellValue("Name").ToString(); string msg = string.Format("지금 선택하신 컬럼의 값 : {0} 입니다.", str); MessageBox.Show(msg); } } } }
|
실행 결과
위와 같이 제가 Name 컬럼에 “범범조조” Cell을 클릭하니까 해당 데이터가 메시지 박스 형태로 가져와서 출력되는 것을 확인하실 수 있습니다.
이로써, RowCellClick이벤트를 이용하여 선택된 셀의 데이터를 가져오는 방법에 대해서 알아 보았습니다.
감사합니다.
'C# > DevExpress' 카테고리의 다른 글
이 글을 공유하기