⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 data binding in _net - c# windows forms.htm

📁 Teach you to use Databinding with CSharp .net.
💻 HTM
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0054)http://www.akadia.com/services/dotnet_databinding.html -->
<HTML><HEAD><TITLE>Data Binding in .NET / C# Windows Forms</TITLE>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<META content="MSHTML 6.00.3790.3123" name=GENERATOR>
<META http-equiv=Content-Language content=en-us>
<META content=FrontPage.Editor.Document name=ProgId>
<META http-equiv=expires content=86400>
<META content="AKADIA Information Technology AG" name=author>
<META content="AKADIA Information Technology AG, Bern, Schweiz" 
name=description>
<META content=global name=distribution><LINK 
href="Data Binding in _NET - C# Windows Forms_files/website.css" type=text/css 
rel=stylesheet></HEAD>
<BODY text=#000000 vLink=#0000ff aLink=#0000ff bgColor=#ffffff leftMargin=20>
<TABLE style="BORDER-COLLAPSE: collapse" borderColor=#111111 cellSpacing=0 
cellPadding=0 width=876 border=0>
  <TBODY>
  <TR>
    <TD vAlign=top width=969 colSpan=2 height=16>&nbsp;&nbsp; </TD></TR>
  <TR>
    <TD vAlign=top width=114><A href="javascript:history.back()"><IMG 
      height=19 alt=Zur點k 
      src="Data Binding in _NET - C# Windows Forms_files/zurueck.gif" width=76 
      border=0></A> </TD>
    <TD vAlign=top width=855>
      <H1><SPAN lang=de-ch>Data Binding in .NET / C# Windows Forms</SPAN> </H1>
      <P><SPAN lang=en-us>More Information on installing the .Net Framework 
      click <A href="http://www.akadia.com/services/dotnet_dbaccess.html" 
      target=_top>here</A>.<BR></SPAN><A 
      href="http://www.akadia.com/download/documents/DataBinding.zip" 
      target=_top>Download</A> full Visual Studio C# .NET Example from this 
      Article. </P>
      <HR color=#34b389 noShade>
    </TD></TR>
  <TR>
    <TD vAlign=top width=894 colSpan=2 height=51>
      <H1><SPAN lang=de-ch>Contents</SPAN> </H1>
      <BLOCKQUOTE>
        <P class=header><A 
        href="http://www.akadia.com/services/dotnet_databinding.html#Overview">Overview</A> 
        </P>
        <P class=header><SPAN lang=en-us><A 
        href="http://www.akadia.com/services/dotnet_databinding.html#Binding%20Context">Binding 
        Context</A></SPAN> </P>
        <P class=header><SPAN lang=en-us><A 
        href="http://www.akadia.com/services/dotnet_databinding.html#CurrencyManager">CurrencyManager</A></SPAN> 
        </P>
        <P class=header><SPAN lang=en-us><A 
        href="http://www.akadia.com/services/dotnet_databinding.html#Bindable%20Data%20Sources">Bindable 
        Data Sources</A></SPAN> </P>
        <BLOCKQUOTE>
          <P><SPAN lang=en-us><A 
          href="http://www.akadia.com/services/dotnet_databinding.html#Array%20or%20Collection">Array 
          or Collection</A><BR><A 
          href="http://www.akadia.com/services/dotnet_databinding.html#ADO.NET%20Data%20Objects">ADO.NET 
          Data Objects</A></SPAN> </P>
          <BLOCKQUOTE>
            <P><SPAN lang=en-us><A 
            href="http://www.akadia.com/services/dotnet_databinding.html#DataColumn">DataColumn</A><B><BR></B><A 
            href="http://www.akadia.com/services/dotnet_databinding.html#DataTable">DataTable</A><BR><A 
            href="http://www.akadia.com/services/dotnet_databinding.html#DataView">DataView</A><BR><A 
            href="http://www.akadia.com/services/dotnet_databinding.html#DataSet">DataSet</A><BR><A 
            href="http://www.akadia.com/services/dotnet_databinding.html#DataViewManager">DataViewManager</A></SPAN> 
            </P></BLOCKQUOTE></BLOCKQUOTE>
        <P class=header><SPAN lang=en-us><A 
        href="http://www.akadia.com/services/dotnet_databinding.html#Example:%20Automatically%20synchronized%20Master-Detail">Example: 
        Automatically synchronized Master-Detail</A></SPAN> </P>
        <BLOCKQUOTE>
          <P><SPAN lang=de-ch><A 
          href="http://www.akadia.com/services/dotnet_databinding.html#Managing%20Data%20Relation">Managing 
          Data Relation</A><BR></SPAN><A 
          href="http://www.akadia.com/services/dotnet_databinding.html#Binding%20to%20a%20DataGrid">Binding 
          to a DataGrid</A><BR><SPAN lang=de-ch><A 
          href="http://www.akadia.com/services/dotnet_databinding.html#Binding%20to%20a%20Combobox">Binding 
          to a Combobox</A></SPAN> </P></BLOCKQUOTE>
        <P class=header><A 
        href="http://www.akadia.com/services/dotnet_databinding.html#Example:%20Forms%20BindingContext%20Position%20Management"><SPAN 
        lang=en-us>Example:</SPAN> Form's BindingContext <SPAN 
        lang=de-ch>Position Management</SPAN></A> </P>
        <BLOCKQUOTE>
          <P><A 
          href="http://www.akadia.com/services/dotnet_databinding.html#Handling%20of%20the%20PositionChanged%20event%20for%20the%20Forms%20BindingContext"><SPAN 
          lang=de-ch>Handling of the</SPAN> PositionChanged event for the Form's 
          BindingContext</A><BR><SPAN lang=en-us><A 
          href="http://www.akadia.com/services/dotnet_databinding.html#Format%20and%20Parse%20Events%20of%20the%20Binding%20Class">Format 
          and Parse Events of the Binding Class</A></SPAN> </P></BLOCKQUOTE>
        <P class=header><A 
        href="http://www.akadia.com/services/dotnet_databinding.html#Example:%20Binding%20to%20a%20ComboBox%20or%20ListBox"><SPAN 
        lang=de-ch>Example:</SPAN> Binding to a ComboBox or ListBox</A> </P>
        <BLOCKQUOTE>
          <P><A 
          href="http://www.akadia.com/services/dotnet_databinding.html#Bind%20a%20ComboBox%20to%20an%20array%20of%20State%20objects"><SPAN 
          lang=de-ch>B</SPAN>ind a ComboBox to an array of State objects</A> 
        </P></BLOCKQUOTE></BLOCKQUOTE>
      <HR color=#34b389 noShade>
    </TD></TR>
  <TR>
    <TD vAlign=top width=894 colSpan=2 height=37>
      <H1><A name=Overview>Overview</A> </H1>
      <BLOCKQUOTE>
        <P>Data binding provides a way for developers to create a read/write 
        link between the controls on a form and the data in their application 
        (their data model). Classically, data binding was used within 
        applications to take advantage of data stored in databases. Windows 
        Forms data binding allows you to access <B>data from databases</B> as 
        well as data in other structures, such as <B>arrays</B> and 
        <B>collections</B><SPAN lang=de-ch>.</SPAN> </P>
        <P align=center><IMG height=242 
        src="Data Binding in _NET - C# Windows Forms_files/dotnet_databinding_1.gif" 
        width=413 border=0> </P></BLOCKQUOTE>
      <H1 align=left><SPAN lang=en-us><A name="Binding Context">Binding 
      Context</A></SPAN> </H1>
      <BLOCKQUOTE>
        <P><SPAN lang=en-us>Each Windows Form has at least one 
        <B>BindingContext</B> object that manages the <B>CurrencyManager</B> 
        objects for the form. <B>For each data source on a Windows Form, there 
        is a single CurrencyManager object.</B> Because there may be multiple 
        data sources associated with a Windows Form, the BindingContext object 
        enables you to retrieve any particular CurrencyManager object associated 
        with a data source.</SPAN> </P></BLOCKQUOTE>
      <P class=header><SPAN lang=en-us>Example</SPAN> </P>
      <BLOCKQUOTE>
        <P><SPAN lang=en-us>For example if you add a TextBox control to a form 
        and bind it to a column of a table (e.g. "Customers.FirstName") in a 
        dataset (e.g. "dsCust"), <B>the control communicates with the 
        BindingContext object for that form</B>. The BindingContext object, in 
        turn, talks to the specific CurrencyManager object for that data 
        association. If you queried the CurrencyManager's Position property, it 
        would report the current record for that TextBox control's binding. In 
        the example below, a TextBox control is bound to the FirstName column of 
        a Customers table on the</SPAN> <FONT size=2><SPAN 
        lang=en-us>dsCust</SPAN></FONT> <SPAN lang=en-us>dataset through the 
        BindingContext object for the form it is on.</SPAN> </P>
        <P class=courier align=left><SPAN lang=en-us><FONT color=#0000ff>// 
        Simple Data Binding</FONT><BR><B>txtBox</B></SPAN><B><SPAN 
        lang=en-us>.DataBindings.Add("Text",</SPAN><FONT size=2><SPAN 
        lang=en-us>dsCust</SPAN></FONT><SPAN 
        lang=en-us>,"Customers.FirstName");</SPAN></B> </P>
        <P class=courier align=left><SPAN lang=en-us><FONT color=#0000ff>// Get 
        current Rowposition</FONT><B><BR></B></SPAN><B><FONT size=2><SPAN 
        lang=en-us>CurrencyManager cm = 
        (CurrencyManager)this.BindingContext[dsCust,"Customers"];<BR>long 
        rowPosition = (long</SPAN></FONT></B><SPAN lang=en-us><FONT 
        size=2><B>)cm.Position;</B></FONT></SPAN> </P></BLOCKQUOTE>
      <H1><SPAN lang=en-us><A name=CurrencyManager>CurrencyManager</A></SPAN> 
      </H1>
      <BLOCKQUOTE>
        <P><SPAN lang=en-us>The CurrencyManager is used to <B>keep data-bound 
        controls synchronized with each other (showing data from the same 
        record).</B> The CurrencyManager object does this by managing a 
        collection of the bound data supplied by a data source. For each data 
        source associated with a Windows Form, the form maintains at least one 
        CurrencyManager. Because there may be more than one data source 
        associated with a form, the BindingContext object manages all of the 
        CurrencyManager objects for any particular form. More broadly, all 
        container controls have at least one BindingContext object to manage 
        their CurrencyManagers.</SPAN> </P>
        <P><SPAN lang=en-us>An <B>important property</B> of the CurrencyManager 
        is the <B>Position</B> property. <I>Currency</I> is a term used to refer 
        to the currentness of position within a data structure. You can use the 
        Position property of the CurrencyManager class to determine the current 
        position of all controls bound to the same CurrencyManager.</SPAN> </P>
        <P><SPAN lang=en-us>For example, imagine a collection consisting of two 
        columns called "ContactName" and "Phone". Two TextBox controls are bound 
        to the same data source. When the Position property of the common 
        CurrencyManager is set to the fourth position within that list 
        (corresponding to the fifth name, because it is zero-based), both 
        controls display the appropriate values (the fifth "ContactName" and the 
        fifth "Phone") for that position in the data source.</SPAN> 
      </P></BLOCKQUOTE>
      <P class=header><SPAN lang=en-us>Example</SPAN> </P>
      <BLOCKQUOTE>
        <P><SPAN lang=en-us>For example the Position property of the 
        CurrencyManager is often manipulated in a Next / Prev Navigation 
        Button.</SPAN> </P>
        <P><IMG height=58 
        src="Data Binding in _NET - C# Windows Forms_files/dotnet_databinding_2.gif" 
        width=304 border=0> </P>
        <P class=courier><SPAN lang=en-us><FONT color=#0000ff>// Position to 
        next Record in Customer</FONT><BR>private void btnNext_Click(object 
        sender, System.EventArgs e)<BR>{<BR></SPAN><B><SPAN 
        lang=en-us>&nbsp;&nbsp;&nbsp; CurrencyManager cm = 
        (CurrencyManager)this.BindingContext[dsCust,"Customers"];<BR>&nbsp;&nbsp;&nbsp; 
        if (cm.Position &lt; cm.Count - 1)<BR>&nbsp;&nbsp;&nbsp; 
        {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
        cm.Position++;<BR>&nbsp;&nbsp;&nbsp; }<BR></SPAN></B><SPAN 
        lang=en-us>}</SPAN> </P></BLOCKQUOTE>
      <H1><SPAN lang=en-us><A name="Bindable Data Sources">Bindable Data 
      Sources</A></SPAN> </H1>
      <BLOCKQUOTE>
        <P><SPAN lang=en-us>In Windows Forms, you can bind to a wide variety of 
        structures, from simple (arrays) to complex (data rows, data views, and 
        so on). <B>As a minimum, a bindable structure must support the IList 
        interface</B>. As structures are based on increasingly capable 
        interfaces, they offer more features that you can take advantage of when 
        data binding. The list below summarizes the type of structures (data 
        containers) you can bind to and provides some notes about what 
        data-binding features are supported.</SPAN> </P></BLOCKQUOTE>
      <P class=header><SPAN lang=en-us><A name="Array or Collection">Array or 
      Collection</A></SPAN> </P>
      <BLOCKQUOTE>
        <P><SPAN lang=en-us>To act as a data source, a list must implement the 
        <B>IList</B> interface; one example would be an array that is an 
        instance of the <I>System.Array</I> class.</SPAN> </P></BLOCKQUOTE>
      <P class=header><SPAN lang=en-us><A name="ADO.NET Data Objects">ADO.NET 
      Data Objects</A></SPAN> </P>
      <BLOCKQUOTE>
        <P><SPAN lang=en-us>ADO.NET provides a number of data structures 
        suitable for binding to:</SPAN> </P>
        <TABLE id=AutoNumber2 style="BORDER-COLLAPSE: collapse" 
        borderColor=#111111 cellSpacing=0 cellPadding=0 width="90%" border=0>
          <TBODY>
          <TR>
            <TD width="100%">
              <UL>
                <LI><SPAN lang=en-us><B><A name=DataColumn>DataColumn</A></B> 
                object 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -