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; }
	}
}

 

 

 

 

 

 

LookUpEdit는 드롭 다운 방식으로 데이터를 조회 및 선택 할 수 있는 컨트롤 입니다.

public class CityInfo 
{
    public int ID { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
    public string Region { get; set; }
}

List<Person> Persons = new List<Person>();
List<CityInfo> Cities = new List<CityInfo>();

CreateData();
            
LookUpEdit lookUpEdit = new LookUpEdit();
lookUpEdit.Parent = this;
lookUpEdit.Location = new System.Drawing.Point(198,78);
lookUpEdit.Size = new System.Drawing.Size(199,20);

lookUpEdit.DataBindings.Add(new Binding("EditValue", Persons, "CityID"));
lookUpEdit.Properties.DataSource = Cities;
lookUpEdit.Properties.ValueMember = "ID";
lookUpEdit.Properties.DisplayMember = "City";
lookUpEdit.Properties.PopulateColumns();+

private void CreateData()
{
      Cities.Add(new CityInfo() { ID = 0, City= "Barquisimeto", Country= "Venezuela", Region="Lara" });
      Cities.Add(new CityInfo() { ID = 1, City = "Rio de Janeiro", Country = "Brazil", Region = "RJ" });
      Cities.Add(new CityInfo() { ID = 2, City = "Cunewalde", Country = "Germany", Region = "" });
      Cities.Add(new CityInfo() { ID = 3, City = "Madrid", Country = "Spain", Region = "" });
      Cities.Add(new CityInfo() { ID = 4, City = "Charleroi", Country = "Belgium", Region = "" });
      Cities.Add(new CityInfo() { ID = 5, City = "Sao Paulo", Country = "Brazil", Region = "SP" });
}

 

 

+ Recent posts