WinForm TextBox数据绑定

 
         private  DataTable dtStore;
        
private  System.Windows.Forms.TextBox txtID;
        
private  System.Windows.Forms.TextBox txtName;
        
private  System.Windows.Forms.BindingManagerBase BindingDataManager;
        
private   void  frmDemo_Load( object  sender, System.EventArgs e)
        
{
            initForm();
        }

        
private   void  btnFirst_Click( object  sender, System.EventArgs e)
        
{
            BindingDataManager.Position 
= 0;
        }


        
private   void  btnPre_Click( object  sender, System.EventArgs e)
        
{
            BindingDataManager.Position 
-= 1;
        }


        
private   void  btnNext_Click( object  sender, System.EventArgs e)
        
{
            BindingDataManager.Position 
+= 1;
        }


        
private   void  btnLast_Click( object  sender, System.EventArgs e)
        
{
            BindingDataManager.Position 
= BindingDataManager.Count -1;
        }

        
private   void  initForm()
        
{
            
string strConn = @"Server=(local)\NETSDK;User id=sa;Pwd=sa;Database=Northwind";
            StringBuilder sbSQL 
= new StringBuilder();
            sbSQL.Append(
"select EmployeeID, FirstName + ' ' + LastName as EmployeeName ");
            sbSQL.Append(
"from Employees");

            SqlDataAdapter da 
= new SqlDataAdapter( sbSQL.ToString(), conn );

            dtStore 
= new DataTable();
        
            da.Fill( dtStore );    
            
            txtID.DataBindings.Clear();
            txtName.DataBindings.Clear();

            txtID.DataBindings.Add( 
"Text", dtStore, dtStore.Columns[0].ColumnName );
            txtName.DataBindings.Add( 
"Text", dtStore, dtStore.Columns[1].ColumnName );

            BindingDataManager 
= this.BindingContext[ dtStore ];
        }

BindingManagerBase是对Windows 窗体上绑定到相同数据源的数据绑定控件进行同步的

=======================================================================

  DataSet ds = DataRepository.UsersProvider.GetAll().ToDataSet(true);
            BindingSource bs = new BindingSource();
            bs.DataSource = ds.Tables[0];

           bindingNavigator1.BindingSource = bs;            
           dataGridView1.DataSource = bs;
           textBox1.DataBindings.Add("Text", bs, ds.Tables[0].Columns[0].ColumnName);
           textBox2.DataBindings.Add("Text",bs, ds.Tables[0].Columns[1].ColumnName);

你可能感兴趣的:(WinForm)