utorok, 12 február 2013 08:23 Written by 1703 times
Rate this item
(3 votes)

C# - Data Binding

DataBinding nám poskytuje spôsob ako vytvoriť spojenie medzi prvkami na formulári ( TextBox, ComboBox,..) a dátami v aplikácii (databáza,List,Array..). Viaže ich dohromady a udržuje synchronizáciu dát. Ak je binding vykonaný správne, tak každá zmena dát sa automatický prejavý na prvku (textbox,...).

 Príklad prepojenia TextBox-u s dátami:

this.textBox1.DataBindings.Add("Text",this.myList,"UserName");

 Vytvoríme si this.myList , ktorý obsahuje list objektov z triedy ClassUser. Ak sa zmení hodnota UserName, tak sa nám to hneď prejaví v TextBox-e a opačne, ak zmeníme údaj v TextBoxe, tak sa nám automaticky zmení aj v UserName.

Môžeme prepojiť aj inú vlastnosť napr. Checked v CheckBoxe:

this.checkBox1.DataBindings.Add("Checked",this.myList,"IsAdmin");

Pri prepojení ComboBox-u s Listom musíme ešte nastaviť DisplayMember :

this.comboBox1.DataSource = this.myList;
this.comboBox1.DisplayMember = "UserName";

Ak chceme prepojiť vlastnosť typu int s TextBoxom :

System.Windows.Forms.Binding b1 = new System.Windows.Forms.Binding("Text",this.myList,"Age");
b1.Format += new ConvertEventHandler(b1_Format);
this.textBox1.DataBindings.Add(b1);

 

		void b1_Format(object sender, ConvertEventArgs e)
		{			
		    if (e.Value.GetType() != typeof(string)) return;
		
		    string value1 = (string)e.Value;
		
		    try
		    {
		        e.Value = Convert.ToInt16(value1);
		    }
		    catch(Exception ex)
		    {
		        MessageBox.Show(ex.Message);
		    }
		}

 

 

binding

 

Trieda ClassUser :

	public class ClassUser
	{
			public string UserName{get;set;}
			public string LoginName{get;set;}
			public string Info{get;set;}
			public int Age{get;set;}
			public bool IsAdmin{get;set;}
	}

Kód v c# :

 

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace Binding
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		private List<ClassUser> myList = new List<ClassUser>();
		public MainForm()
		{
			InitializeComponent();
			defaultData();
			bindingAll();
		}
		private void bindingAll()
		{
			this.textBox1.DataBindings.Add("Text",this.myList,"UserName");
			this.textBox2.DataBindings.Add("Text",this.myList,"LoginName");
			this.richTextBox1.DataBindings.Add("Text",this.myList,"Info");
			this.comboBox1.DataSource = this.myList;
			this.comboBox1.DisplayMember = "UserName";
			this.listBox1.DataSource = this.myList;
			this.listBox1.DisplayMember = "LoginName";			
			this.checkBox1.DataBindings.Add("Checked",this.myList,"IsAdmin");
			
			//DataGridView	
			DataGridViewTextBoxColumn userColumn = new DataGridViewTextBoxColumn();
			userColumn.DataPropertyName = "UserName";
			userColumn.HeaderText = "Header user name";
			
			DataGridViewTextBoxColumn loginColumn = new DataGridViewTextBoxColumn();
			loginColumn.DataPropertyName = "LoginName";
			loginColumn.HeaderText = "Login name";
			
			DataGridViewTextBoxColumn infoColumn = new DataGridViewTextBoxColumn();
			infoColumn.DataPropertyName = "Info";
			infoColumn.HeaderText = "Info";
			
			DataGridViewTextBoxColumn ageColumn = new DataGridViewTextBoxColumn();
			ageColumn.DataPropertyName = "Age";
			ageColumn.HeaderText = "Age";
			
			
			DataGridViewCheckBoxColumn adminColumn = new DataGridViewCheckBoxColumn();
			adminColumn.DataPropertyName = "IsAdmin";
			adminColumn.HeaderText = "Admin";
			this.dataGridView1.Columns.Add(userColumn);
			this.dataGridView1.Columns.Add(loginColumn);
			this.dataGridView1.Columns.Add(infoColumn);
			this.dataGridView1.Columns.Add(ageColumn);
			this.dataGridView1.Columns.Add(adminColumn);
			
			this.dataGridView1.DataSource = this.myList;
			
		}
		private void defaultData()
		{
			
			
			ClassUser user1 = new ClassUser();
			user1.UserName ="user1";
			user1.LoginName ="user1Login";
			user1.Info = "Info about user1";
			user1.Age = 15;
			user1.IsAdmin = true;
			myList.Add(user1);
			
			ClassUser user2 = new ClassUser();
			user2.UserName ="user2";
			user2.LoginName ="user2Login";
			user2.Info = "Info about user2";
			user2.Age = 25;
			user2.IsAdmin = true;
			myList.Add(user2);
			
			ClassUser user3 = new ClassUser();
			user3.UserName ="Ferko";
			user3.LoginName ="Ferko33";
			user3.Info = "Info about Ferko";
			user3.Age = 31;
			user3.IsAdmin = false;
			myList.Add(user3);
		}
	}
}

 

 

Last modified on pondelok, 19 december 2016 21:21
Ing.Jaroslav Vadel

Som zakladateľom www.projectik.eu.

Hram sa na programatora, ktory ovlada c#,cpp,java,unity3d,php,html,NI testand... ..."Naprogramovat program,ktory funguje vie kazdy. Ale to,ze program funguje este neznamena,ze je napisany spravne "..

Website: www.projectik.eu