afsdzvcx123.tistory.com
[C# FarPoint] C# FarPoint Spread Sheet 컨트롤 Cell Click 이벤트 선언하기(현재 Row 구하기)안녕하세요. 오늘은 C# FarPoint 두 번째 시간으로서, Spread Sheet Cell Click 이벤트 선언하는 방법에 대해서 알려 드리려고 합니다. FarPoint 컨트롤을 이용하여 작업하다 보면 Cell Click 이벤트를 다루는 경우가 종종 있습니다. 저 같은 경우에는 제가 현재 클릭한 Row가 몇 번째 줄인지를 Cell Click 이벤트를 통해서 구현하는 편입니다. 그럼 예제 코드를 통해서 직접 Cell Click 이벤트를 선언해서 현재 제가 몇 번째 Row를 클릭했는지 반환하는 프로그램을 만들어 보도록 하겠습니다. 빈 윈폼 프로젝트 선언 및 FarPoint 컨트롤 배치 위와 같이 빈 윈폼 프로젝트에 FarPoint Sheet 컨트롤을 하나 배치 하였습니다. 그럼 이제 Cell Click..
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 Test
{
public partial class Form1 : Form
{
public DataTable dt = null;
private List<string> ColumnList = new List<string>();
private int activeRow = 0;
public enum COLUMNS
{
NAME, AGE, GRADE, PHONE_NUMBER
}
public Form1()
{
InitializeComponent();
this.Load += TestForm_Load;
}
/// <summary>
/// Form Load 이벤트 핸들러
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void TestForm_Load(object sender, EventArgs e)
{
InitFrm();
}
/// <summary>
/// 설정 메서드
/// </summary>
public void InitFrm()
{
ColumnList = Enum.GetNames(typeof(COLUMNS)).ToList(); //컬럼명 설정
//테스트 데이터 설정
GetDataTable();
//Sheet 설정
SetSheet();
//Sheet에 데이터 넣기
Set_Data();
}
/// <summary>
/// 테스트 데이터 넣기
/// </summary>
public void GetDataTable()
{
dt = new DataTable(); //DataTable 객체 생성
dt.Columns.Add("NAME"); //컬럼 생성
dt.Columns.Add("AGE"); //컬럼 생성
dt.Columns.Add("GRADE"); //컬럼 생성
dt.Columns.Add("PHONE_NUMBER"); //컬럼 생성
dt.Rows.Add("범범조조", "28", "4", "111-2222-3444");
dt.Rows.Add("아이유", "28", "4", "112-2812-3444");
dt.Rows.Add("백예린", "24", "3", "113-5622-044");
dt.Rows.Add("태연", "31", "2", "114-2222-3404");
}
/// <summary>
/// Sheet 설정 및 컬럼명 설정
/// </summary>
private void SetSheet()
{
//Cell Click 이벤트 선언
fpSpread.CellClick -= fpSpread_CellClick;
fpSpread.CellClick += fpSpread_CellClick;
// Ctrl 로 다중 선택
fpSpread.ActiveSheet.SelectionPolicy = FarPoint.Win.Spread.Model.SelectionPolicy.MultiRange;
Sheet_Main.SelectionUnit = FarPoint.Win.Spread.Model.SelectionUnit.Row;
Sheet_Main.OperationMode = FarPoint.Win.Spread.OperationMode.ReadOnly;
// 다중 선택 시 Color 설정
Sheet_Main.SelectionStyle = FarPoint.Win.Spread.SelectionStyles.SelectionColors;
Sheet_Main.SelectionBackColor = Color.FromArgb(255, 150, 150, 255);
Sheet_Main.ColumnHeader.Rows[0].Height = 30;
Sheet_Main.Columns.Count = dt.Columns.Count;
for (int col = 0; col < Sheet_Main.Columns.Count; col++)
{
// 컬럼헤더명
string tmpColumnHeader = ColumnList[col];
Sheet_Main.ColumnHeader.Cells[0, col].Text = tmpColumnHeader.Replace("_", " ");
Sheet_Main.Columns[col].Width = 150;
// 가운데 정렬
Sheet_Main.Columns[col].VerticalAlignment = FarPoint.Win.Spread.CellVerticalAlignment.Center;
Sheet_Main.Columns[col].HorizontalAlignment = FarPoint.Win.Spread.CellHorizontalAlignment.Center;
}
for (int row = 0; row < Sheet_Main.Rows.Count; ++row)
Sheet_Main.Rows[row].BackColor = row % 2 == 1 ? Color.FromArgb(255, 208, 230, 252) : Sheet_Main.DefaultStyle.BackColor;
fpSpread.HorizontalScrollBarPolicy = FarPoint.Win.Spread.ScrollBarPolicy.AsNeeded;
fpSpread.VerticalScrollBarPolicy = FarPoint.Win.Spread.ScrollBarPolicy.AsNeeded;
}
/// <summary>
/// Sheet에 Row 데이터 넣기
/// </summary>
public void Set_Data()
{
Sheet_Main.Columns.Count = dt.Columns.Count; // Sheet Columns 카운트 설정
Sheet_Main.Rows.Count = 0; //Sheet Row 카운트 설정
for (int row = 0; row < dt.Rows.Count; row++)
{
Sheet_Main.Rows.Count++;
for (int col = 0; col < Sheet_Main.Columns.Count; col++)
{
Sheet_Main.Cells[row, col].Value = dt.Rows[row][col].ToString();
}
}
}
/// <summary>
/// Cell Click 이벤트 핸들러
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void fpSpread_CellClick(object sender, FarPoint.Win.Spread.CellClickEventArgs e)
{
this.activeRow = e.Row; //클릭한 Row의 줄 반환
string msg = string.Format("현재 클릭한 Row는 {0} 번째 줄입니다.", this.activeRow + 1);
MessageBox.Show(msg);
}
}
}