数据绑定

                                                                          简单数据绑定

在新建的工程中添加㙬个文本框。
首先要建立与数据源的连接,用结果集填充创建的DataSet。一旦提取了所有的数据,就进行绑定:
textBox1.DataBindings.Add("Text",ds,"Employees.FirstName");
由于TextBox控件一次只能显示一个值,所以,使用Add方法将其Text属性绑定到Employees表的FirstName列。
Add方法采用下面的参数:
         要绑定到的属性的名称
         数据源
         字符串文字,数据成员,用来描述被绑定值的准确位置
这里使用DataSet作数据源,数据成员只能是Table.Column形式的路径。

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;
using  System.Data.SqlClient;
namespace  Banding
{
    
public partial class Form1 : Form
    
{
        
public Form1()
        
{
            InitializeComponent();
        }


        
private void Form1_Load(object sender, EventArgs e)
        
{
            
string str = @"server=localhost;Integrated Security=SSPI;database=Northwind";

            
string SQL = "SELECT * FROM Employees";
            SqlConnection conn 
= new SqlConnection(str);
            SqlDataAdapter da 
= new SqlDataAdapter(SQL, conn);
            DataSet ds 
= new DataSet();

            da.Fill(ds, 
"Employees");
            textBox1.DataBindings.Add(
"Text", ds, "Employees.FirstName");
            textBox2.DataBindings.Add(
"Text",ds,"Employees.LastName");

        }

    }

}

                                               复杂数据绑定
  1.绑定到数据表:
新建一个工程,在窗体中添加一个列表框,两个文本框控件。
通过设置DataScoure和DisplayMember属性绑定ListBox控件。
DataScource属性采用集合对象作为数据源,如DataTable或DataSet。
DisplayMember属性采用数据源内的数据成员,用数据填充ListBox。
这里使用DataTable作为数据源,所以DisplayMember属性采用列名绑定到控件。
类似地,通过给DataBindings集合添加绑定以及将DataTable中的列名作为数据成员,把文本绑定到DataTable上:
         textBox.DataBingdings.Add("Text",myTable,"FirstName");
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;
using  System.Data.SqlClient;


namespace  Banding
{
    
public partial class Form1 : Form
    
{
        
public Form1()
        
{
            InitializeComponent();
          
        }


        
private void Form1_Load(object sender, EventArgs e)
        
{
            
           
         
            
string conStr = @"server=localhost;integrated security=SSPI;database=Northwind";

            
//从Employees表中获取数据
            string SQL = "SELECT * FROM Employees";
            SqlConnection Conn 
= new SqlConnection(conStr);
            SqlDataAdapter da 
= new SqlDataAdapter(SQL, Conn);
            
//填充数据
            da.Fill(dataSet1, "Employees");

            DataTable myTable 
= dataSet1.Tables["Employees"];
            
               
//把列表框绑定到对象上
            listBox1.DataSource = myTable;
            listBox1.DisplayMember 
= "FirstName";
               
//把文本框绑定到对象上
            textBox1.DataBindings.Add("Text", myTable, "Title");
            textBox2.DataBindings.Add(
"Text", myTable, "Address");
          
          

        }


        
private void bindingSource1_CurrentChanged(object sender, EventArgs e)
        
{
           
        }


        
private void btnBack_Click(object sender, EventArgs e)
        
{
           
        }


        
private void bindingNavigatorMoveFirstItem_Click(object sender, EventArgs e)
        
{
           
        }

    }

}


             2.绑定到数据集
将DataSet作为数据源,这个类实现IListSource接口。

   private   void  Form1_Load( object  sender, EventArgs e)
        
   
           
         
            
string conStr = @"server=localhost;integrated security=SSPI;database=Northwind";

            
//从Employees表中获取数据
            string SQL = "SELECT * FROM Employees";
            SqlConnection Conn 
= new SqlConnection(conStr);
            SqlDataAdapter da 
= new SqlDataAdapter(SQL, Conn);
            
//绑定到数据集
            da.Fill(dataSet1, "Employees");
            label1.DataBindings.Add(
"Text", dataSet1, "Employees.Title");
            label2.DataBindings.Add(
"Text", dataSet1, "Employees.Address");

            }  
               

                  3.绑定到数据视图
   private   void  Form1_Load( object  sender, EventArgs e)
        
{
            
           
         
            
string conStr = @"server=localhost;integrated security=SSPI;database=Northwind";

            
//从Employees表中获取数据
            string SQL = "SELECT * FROM Employees";
            SqlConnection Conn 
= new SqlConnection(conStr);
            SqlDataAdapter da 
= new SqlDataAdapter(SQL, Conn);
            
//绑定到数据集
            da.Fill(dataSet1, "Employees");
            DataTable tb
=dataSet1.Tables["Employees"];
            
//创建数据视图,设置过滤器,及排序
            DataView dv = new DataView(tb, "Country='UK'""FirstName", DataViewRowState.CurrentRows);
            
//绑定到文本框
            label1.DataBindings.Add("Text", dv, "Title");
            label2.DataBindings.Add(
"Text", dv, "Address");
         }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值