C\#에서 DataBindings 용법 실례 분석

16983 단어 C#DataBindings
본 논문 의 사례 는 C\#에서 DataBindings 의 용법 을 서술 하 였 다.여러분 께 참고 하도록 공유 하 겠 습 니 다.구체 적 으로 는 다음 과 같 습 니 다.
C\#데이터 베 이 스 를 조작 하 는 과정 에서 일반적인 텍스트 컨트롤,예 를 들 어 TextBox,Label 등 을 대상 으로 저 희 는 TextBox.Text=***와 유사 한 방식 으로 직접 할당 합 니 다.이런 방식 은 특정한 의미 에서 가장 간편 한 방식 이지 만 복잡 한 공간,예 를 들 어 DataGridView 등 이 있 을 때바 인 딩 데이터 원본 은 일반적으로 DataGridView 1.DataSource=***방식 으로 진행 되 며,데이터 원본 이 조금 변경 되 었 다 면 바 인 딩 을 다시 호출 하면 됩 니 다.이런 방식 은 단 방향,즉 데이터베이스 에서 UI 까지 라 고 할 수 있 지만 데이터 원본 의 변 화 를 실현 할 수 있 는 방법 이 없 을 때 DataGridView 를 다시 연결 하지 않 아 도 자동 으로 데 이 터 를 새로 고 칠 수 있 습 니 다.물론 여기 서 언급 해 야 할 것 은 DataBinding 입 니 다.
코드 는 다음 과 같다.
Form2.cs 코드:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DataBindingsTest
{
  public partial class Form2 : Form
  {
    public Form2()
    {
      InitializeComponent();
    }
    MyDataSource mydatasource = new MyDataSource(); //        
    public int Num { get; set; } //        
    public List<BlogNew> blogNews { get; set; } //        
    public BindingList<BlogNew> blogNewsRegardUI { get; set; } //   DataGridView  UI   
    private void mainFrm_Load(object sender, EventArgs e)
    {
      #region    
      /************************************************
       *     :    TextBox     
       *     :      
       *     :           
       *     :         
       *     :            
       * *********************************************/
      textBox1.DataBindings.Add("Text", trackBar1, "Value", false, DataSourceUpdateMode.OnPropertyChanged);
      #endregion
      #region    
      /*********************************************
       *               ,     
       * *********************************************/
      mydatasource.Myvalue = "     ";
      textBox2.DataBindings.Add("Text", mydatasource, "Myvalue", false, DataSourceUpdateMode.OnPropertyChanged);
      #endregion
      #region    
      /*****************************************
       *               ,     
       ****************************************/
      Num = 5;
      textBox3.DataBindings.Add("Text", this, "Num", false, DataSourceUpdateMode.OnPropertyChanged);
      #endregion
      /*
       *   :   3   ,        ,               
       *      ,          ,          
       */
      #region     : List<T>
      blogNews = new List<BlogNew>();
      blogNews.Add(new BlogNew { BlogID = 1, BlogTitle = "       " });
      blogNews.Add(new BlogNew { BlogID = 2, BlogTitle = "       " });
      blogNews.Add(new BlogNew { BlogID = 3, BlogTitle = "       " });
      dataGridView1.DataBindings.Add("DataSource", this, "blogNews", false, DataSourceUpdateMode.OnPropertyChanged);
      #endregion
      #region     : BindingList<T>
      blogNewsRegardUI = new BindingList<BlogNew>();
      blogNewsRegardUI.Add(new BlogNew { BlogID = 11, BlogTitle = "       " });
      blogNewsRegardUI.Add(new BlogNew { BlogID = 12, BlogTitle = "       " });
      blogNewsRegardUI.Add(new BlogNew { BlogID = 13, BlogTitle = "       " });
      dataGridView2.DataBindings.Add("DataSource", this, "blogNewsRegardUI", false, DataSourceUpdateMode.OnPropertyChanged);
      #endregion
    }
    private void button1_Click(object sender, EventArgs e)
    {
      //       ,   TextBox2   ,        ,            
      MessageBox.Show(mydatasource.Myvalue);
    }
    private void button2_Click(object sender, EventArgs e)
    {
      //       ,   TextBox3   ,        ,
      //   Num             (         ),       
      MessageBox.Show(Num.ToString());
      //this.Num = 10;
      //MessageBox.Show(Num.ToString());
    }
    private void button3_Click(object sender, EventArgs e)
    {
      //    DataGridView     
      var data = dataGridView1.DataSource as List<BlogNew>;
      data.Add(new BlogNew { BlogID = 4, BlogTitle = "       ,       " });
      foreach (BlogNew blogNew in dataGridView1.DataSource as List<BlogNew>)
      {
        /***********
         *         BlogID   4      ,        dataGridView1 dataSource     ,
         *           BlogID 1,2,3    ,   
         * *********************/
        MessageBox.Show(blogNew.BlogID + "--" + blogNew.BlogTitle);
      }
    }
    private void button4_Click(object sender, EventArgs e)
    {
      /*        DataGridView1        ,         List<BlogNew>,      BindList<BlogNew>
       *    ,    ,       ,             ,    ,       
       */
      var dataRegardUI = dataGridView2.DataSource as BindingList<BlogNew>;
      dataRegardUI.Add(new BlogNew { BlogID = 20, BlogTitle = "       ,       " });
    }
  }
  public class MyDataSource
  {
    public string Myvalue { get; set; }
  }
  public class BlogNew
  {
    public int BlogID { get; set; }
    public string BlogTitle { get; set; }
  }
}

Form2.Designer.cs 코드:

namespace DataBindingsTest
{
  partial class Form2
  {
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
      if (disposing && (components != null))
      {
        components.Dispose();
      }
      base.Dispose(disposing);
    }
    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
      this.groupBox1 = new System.Windows.Forms.GroupBox();
      this.groupBox2 = new System.Windows.Forms.GroupBox();
      this.button1 = new System.Windows.Forms.Button();
      this.textBox2 = new System.Windows.Forms.TextBox();
      this.groupBox3 = new System.Windows.Forms.GroupBox();
      this.button2 = new System.Windows.Forms.Button();
      this.textBox3 = new System.Windows.Forms.TextBox();
      this.groupBox4 = new System.Windows.Forms.GroupBox();
      this.button3 = new System.Windows.Forms.Button();
      this.dataGridView1 = new System.Windows.Forms.DataGridView();
      this.groupBox5 = new System.Windows.Forms.GroupBox();
      this.button4 = new System.Windows.Forms.Button();
      this.dataGridView2 = new System.Windows.Forms.DataGridView();
      this.textBox1 = new System.Windows.Forms.TextBox();
      this.trackBar1 = new System.Windows.Forms.TrackBar();
      this.groupBox1.SuspendLayout();
      this.groupBox2.SuspendLayout();
      this.groupBox3.SuspendLayout();
      this.groupBox4.SuspendLayout();
      ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
      this.groupBox5.SuspendLayout();
      ((System.ComponentModel.ISupportInitialize)(this.dataGridView2)).BeginInit();
      ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit();
      this.SuspendLayout();
      // 
      // groupBox1
      // 
      this.groupBox1.Controls.Add(this.trackBar1);
      this.groupBox1.Controls.Add(this.textBox1);
      this.groupBox1.Location = new System.Drawing.Point(12, 12);
      this.groupBox1.Name = "groupBox1";
      this.groupBox1.Size = new System.Drawing.Size(200, 100);
      this.groupBox1.TabIndex = 0;
      this.groupBox1.TabStop = false;
      this.groupBox1.Text = "   ";
      // 
      // groupBox2
      // 
      this.groupBox2.Controls.Add(this.button1);
      this.groupBox2.Controls.Add(this.textBox2);
      this.groupBox2.Location = new System.Drawing.Point(218, 12);
      this.groupBox2.Name = "groupBox2";
      this.groupBox2.Size = new System.Drawing.Size(200, 100);
      this.groupBox2.TabIndex = 2;
      this.groupBox2.TabStop = false;
      this.groupBox2.Text = "   ";
      // 
      // button1
      // 
      this.button1.Location = new System.Drawing.Point(22, 59);
      this.button1.Name = "button1";
      this.button1.Size = new System.Drawing.Size(157, 23);
      this.button1.TabIndex = 3;
      this.button1.Text = "           ";
      this.button1.UseVisualStyleBackColor = true;
      this.button1.Click += new System.EventHandler(this.button1_Click);
      // 
      // textBox2
      // 
      this.textBox2.Location = new System.Drawing.Point(22, 20);
      this.textBox2.Name = "textBox2";
      this.textBox2.Size = new System.Drawing.Size(157, 21);
      this.textBox2.TabIndex = 2;
      // 
      // groupBox3
      // 
      this.groupBox3.Controls.Add(this.button2);
      this.groupBox3.Controls.Add(this.textBox3);
      this.groupBox3.Location = new System.Drawing.Point(428, 12);
      this.groupBox3.Name = "groupBox3";
      this.groupBox3.Size = new System.Drawing.Size(200, 100);
      this.groupBox3.TabIndex = 4;
      this.groupBox3.TabStop = false;
      this.groupBox3.Text = "   ";
      // 
      // button2
      // 
      this.button2.Location = new System.Drawing.Point(22, 59);
      this.button2.Name = "button2";
      this.button2.Size = new System.Drawing.Size(157, 23);
      this.button2.TabIndex = 3;
      this.button2.Text = "           ";
      this.button2.UseVisualStyleBackColor = true;
      this.button2.Click += new System.EventHandler(this.button2_Click);
      // 
      // textBox3
      // 
      this.textBox3.Location = new System.Drawing.Point(22, 20);
      this.textBox3.Name = "textBox3";
      this.textBox3.Size = new System.Drawing.Size(157, 21);
      this.textBox3.TabIndex = 2;
      // 
      // groupBox4
      // 
      this.groupBox4.Controls.Add(this.button3);
      this.groupBox4.Controls.Add(this.dataGridView1);
      this.groupBox4.Location = new System.Drawing.Point(12, 118);
      this.groupBox4.Name = "groupBox4";
      this.groupBox4.Size = new System.Drawing.Size(568, 157);
      this.groupBox4.TabIndex = 5;
      this.groupBox4.TabStop = false;
      this.groupBox4.Text = "   ";
      // 
      // button3
      // 
      this.button3.Location = new System.Drawing.Point(377, 122);
      this.button3.Name = "button3";
      this.button3.Size = new System.Drawing.Size(157, 23);
      this.button3.TabIndex = 4;
      this.button3.Text = "    ";
      this.button3.UseVisualStyleBackColor = true;
      this.button3.Click += new System.EventHandler(this.button3_Click);
      // 
      // dataGridView1
      // 
      this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
      this.dataGridView1.Location = new System.Drawing.Point(18, 20);
      this.dataGridView1.Name = "dataGridView1";
      this.dataGridView1.RowTemplate.Height = 23;
      this.dataGridView1.Size = new System.Drawing.Size(516, 96);
      this.dataGridView1.TabIndex = 0;
      // 
      // groupBox5
      // 
      this.groupBox5.Controls.Add(this.button4);
      this.groupBox5.Controls.Add(this.dataGridView2);
      this.groupBox5.Location = new System.Drawing.Point(12, 281);
      this.groupBox5.Name = "groupBox5";
      this.groupBox5.Size = new System.Drawing.Size(568, 162);
      this.groupBox5.TabIndex = 6;
      this.groupBox5.TabStop = false;
      this.groupBox5.Text = "   ";
      // 
      // button4
      // 
      this.button4.Location = new System.Drawing.Point(377, 127);
      this.button4.Name = "button4";
      this.button4.Size = new System.Drawing.Size(157, 23);
      this.button4.TabIndex = 4;
      this.button4.Text = "    ";
      this.button4.UseVisualStyleBackColor = true;
      this.button4.Click += new System.EventHandler(this.button4_Click);
      // 
      // dataGridView2
      // 
      this.dataGridView2.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
      this.dataGridView2.Location = new System.Drawing.Point(18, 20);
      this.dataGridView2.Name = "dataGridView2";
      this.dataGridView2.RowTemplate.Height = 23;
      this.dataGridView2.Size = new System.Drawing.Size(516, 91);
      this.dataGridView2.TabIndex = 0;
      // 
      // textBox1
      // 
      this.textBox1.Location = new System.Drawing.Point(18, 20);
      this.textBox1.Name = "textBox1";
      this.textBox1.Size = new System.Drawing.Size(157, 21);
      this.textBox1.TabIndex = 0;
      // 
      // trackBar1
      // 
      this.trackBar1.Location = new System.Drawing.Point(18, 47);
      this.trackBar1.Name = "trackBar1";
      this.trackBar1.Size = new System.Drawing.Size(157, 45);
      this.trackBar1.TabIndex = 1;
      // 
      // Form2
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(676, 471);
      this.Controls.Add(this.groupBox5);
      this.Controls.Add(this.groupBox4);
      this.Controls.Add(this.groupBox3);
      this.Controls.Add(this.groupBox2);
      this.Controls.Add(this.groupBox1);
      this.Name = "Form2";
      this.Text = "Form2";
      this.Load += new System.EventHandler(this.mainFrm_Load);
      this.groupBox1.ResumeLayout(false);
      this.groupBox1.PerformLayout();
      this.groupBox2.ResumeLayout(false);
      this.groupBox2.PerformLayout();
      this.groupBox3.ResumeLayout(false);
      this.groupBox3.PerformLayout();
      this.groupBox4.ResumeLayout(false);
      ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
      this.groupBox5.ResumeLayout(false);
      ((System.ComponentModel.ISupportInitialize)(this.dataGridView2)).EndInit();
      ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).EndInit();
      this.ResumeLayout(false);
    }
    #endregion
    private System.Windows.Forms.GroupBox groupBox1;
    private System.Windows.Forms.GroupBox groupBox2;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.TextBox textBox2;
    private System.Windows.Forms.GroupBox groupBox3;
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.TextBox textBox3;
    private System.Windows.Forms.GroupBox groupBox4;
    private System.Windows.Forms.Button button3;
    private System.Windows.Forms.DataGridView dataGridView1;
    private System.Windows.Forms.GroupBox groupBox5;
    private System.Windows.Forms.Button button4;
    private System.Windows.Forms.DataGridView dataGridView2;
    private System.Windows.Forms.TrackBar trackBar1;
    private System.Windows.Forms.TextBox textBox1;
  }
}

효과 그림:

더 많은 C\#관련 내용 에 관심 이 있 는 독 자 는 본 사이트 의 주 제 를 볼 수 있다.
본 고 에서 말 한 것 이 여러분 의 C\#프로 그래 밍 에 도움 이 되 기 를 바 랍 니 다.

좋은 웹페이지 즐겨찾기