.NET中資料繫結控制項的資料擷取

來源:互聯網
上載者:User

.NET中資料繫結的應用是非常廣泛的,比如datagrid,你可以給他綁定DataTable,也可以綁定一個ArrayList,其實,只要是繼承了介面 IEnumerable 的類型,都可以進行資料繫結,那麼,我們自己寫控制項的時候,如何也實現這種綁定了,其實很簡單,就拿datagrid的 datasource來說
datagrid.DataSource=DataTable.DefaultView;
其實在程式內部,是這麼實現的,首先定義一個變數private IEnumerable _datasource;
然後,增加屬性
public  IEnumerable DataSource
{
    get
    {return _datasource;}
    set
    {_datasource=value;}
}
在進行實際資料繫結的時候,程式可以DataBinder.GetPropertyValue來擷取枚舉資料來源的值,如下實現:
IEnumerator _dataitems=_datasource.GetEnumerator();   //擷取迭代器
 while (_dataitems.MoveNext())
    {
        output.Writln(DataBinder.GetPropertyValue(_dataitems.Current,“name“));    //name就是屬性名稱,比如Datatable中的欄位名或是對象的屬性名稱等等
    }
是不是很簡單,在實際項目中,是經常有機會要自己些控制項並能進行資料繫結的,.NET中內建的 DataBinder 是個功能非常強大的模組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.