본문 바로가기
C#/DevExpress

DevExpress / Winform GridLookUpEdit 컨트롤에 새로운 값을 추가하기.

by HyunS_ 2019. 4. 18.
728x90

GridLookUpEdit 컨트롤에 새로운 값을 추가할 수 있습니다.

 

기존의 존재하는 값을 사용하거나 새로운 문자열을 입력할 수 있습니다.

 

using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsFormsApp4
{
	public partial class Form1 : Form
	{
		#region Form1()

		/// <summary>
		/// 생성자 입니다.
		/// </summary>
		public Form1()
		{
			InitializeComponent();

			InitLookUpEdit();
			InitGridLookUpEdit();
		}

		#endregion

		#region InitLookUpEdit()

		/// <summary>
		/// LookUpEdit를 설정 합니다.
		/// </summary>
		private void InitLookUpEdit()
		{
			this.lookUpEdit1.EditValueChanged += lookUpEdit1_EditValueChanged;

			this.lookUpEdit1.Properties.NullText = "(Select or Type Value)";

			string[] colors = new string[]
			{
				"Yellow", "Red", "Green", "Black", "White"
			};

			this.lookUpEdit1.Properties.DataSource				   = colors;

			this.lookUpEdit1.Properties.TextEditStyle			   = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;

			this.lookUpEdit1.Properties.AcceptEditorTextAsNewValue = DevExpress.Utils.DefaultBoolean.Default;
		}

		#endregion

		#region InitGridLookUpEdit()

		/// <summary>
		/// GridLookUpEdit을 설정 합니다.
		/// </summary>
		private void InitGridLookUpEdit()
		{
			this.gridLookUpEdit1.EditValueChanged    += gridLookUpEdit1_EditValueChanged;

			this.gridLookUpEdit1.Properties.NullText = "(Select or Type Value)";

			List<Sample> item = new List<Sample>
			{
				new Sample(){ Name = "가"},
				new Sample(){ Name = "나"},
				new Sample(){ Name = "다"},
				new Sample(){ Name = "라"},
				new Sample(){ Name = "마"},
				new Sample(){ Name = "바"}
			};

			this.gridLookUpEdit1.Properties.DataSource				   = item;
			this.gridLookUpEdit1.Properties.TextEditStyle		       = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;
			this.gridLookUpEdit1.Properties.AcceptEditorTextAsNewValue = DevExpress.Utils.DefaultBoolean.True;
			this.gridLookUpEdit1.Properties.ValueMember				   = "Name";
			this.gridLookUpEdit1.Properties.DisplayMember			   = this.gridLookUpEdit1.Properties.ValueMember;
			this.gridLookUpEdit1.ProcessNewValue					  += gridLookUpEdit1_ProcessNewValue;
		}

		Dictionary<LookUpEditBase, LabelControl> labelDictionaryCore;
		Dictionary<LookUpEditBase, LabelControl> labelDictionary
		{
			get 
			{
				if (labelDictionaryCore == null)
				{
					labelDictionaryCore = new Dictionary<LookUpEditBase, LabelControl>();
					labelDictionaryCore.Add(lookUpEdit1,     labelControl1);
					labelDictionaryCore.Add(gridLookUpEdit1, labelControl2);
				}

				return labelDictionaryCore;
			}	
		}


		#region gridLookUpEdit1_ProcessNewValue(sender, e)

		/// <summary>
		/// GridLookUpEdit에 새로운 값을 추가합니다.
		/// </summary>
		/// <param name="sender">이벤트 발생자입니다.</param>
		/// <param name="e">이벤트 인자입니다.</param>
		private void gridLookUpEdit1_ProcessNewValue(object sender, DevExpress.XtraEditors.Controls.ProcessNewValueEventArgs e)
		{
			GridLookUpEdit gridLookup = sender as GridLookUpEdit;

			if (e.DisplayValue == null)
			{ 
				return;
			} 

            string newValue = e.DisplayValue.ToString();

			if (newValue == String.Empty)
			{ 
				return;
			} 
            if (MessageBox.Show(this, "Add '" + newValue + "' to list?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes) 
			{
                List<Sample> ds = gridLookup.Properties.DataSource as List<Sample>;
                ds.Add(new Sample { Name = newValue });
                e.Handled = true;
            }
		}



		#endregion

		#region lookUpEdit1_EditValueChanged(sender, e)

		/// <summary>
		/// GridLookUpEdit의 값이 변경됩니다.
		/// </summary>
		/// <param name="sender">이벤트 발생자입니다.</param>
		/// <param name="e">이벤트 인자입니다.</param>
		private void gridLookUpEdit1_EditValueChanged(object sender, EventArgs e)
		{
			LookUpEditBase lookupEditor = sender as LookUpEditBase;

			if (lookupEditor == null)
			{ 
				return;
			} 
            LabelControl label = labelDictionary[lookupEditor];

			if (label == null) 
			{
				return;
			}
			if (lookupEditor.EditValue == null)
			{
				label.Text = "Current EditValue: null";
			}
			else
			{ 
				label.Text = "Current EditValue: " + lookupEditor.EditValue.ToString();
			}
                
		}

		#endregion


		#region lookUpEdit1_EditValueChanged(sender, e)

		/// <summary>
		/// LookUpEdit의 값이 변경될 때 동작합니다.
		/// </summary>
		/// <param name="sender">이벤트 발생자입니다.</param>
		/// <param name="e">이벤트 인자입니다.</param>
		private void lookUpEdit1_EditValueChanged(object sender, EventArgs e)
		{
			LookUpEditBase lookupEditor = sender as LookUpEditBase;

			if (lookupEditor == null)
			{ 
				return;
			}
            
			LabelControl label = labelDictionary[lookupEditor];

			if (label == null) 
			{ 
				return;
			}
			if (lookupEditor.EditValue == null)
			{
				label.Text = "Current EditValue: null";
			}
			else
			{ 
				label.Text = "Current EditValue: " + lookupEditor.EditValue.ToString();
			}
                
        }

		#endregion

	}

	#endregion

	/// <summary>
	/// Sample 클래스 입니다.
	/// </summary>
	public class Sample
	{ 
		/// <summary>
		/// 이름 입니다.
		/// </summary>
		public string Name { get; set; }
	}
}

 

 

 

 

 

 

728x90

댓글