熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

C#高級編程:數據綁定對象[4]

2013-11-13 12:16:30  來源: .NET編程 
    ——此文章摘自《C#高級編程(第版)》定價元 特價元 購買

          textQuanAnchor = AnchorStylesTop | AnchorStylesLeft |
                            AnchorStylesTop;
          textQuanSize = new SystemDrawingSize( );
          textQuanEnabled = false;
          thisControlsAdd(thistextQuan);
          thistrackBar = new TrackBar();
          trackBarBeginInit();
          trackBarDock = DockStyleBottom ;
          trackBarLocation = new SystemDrawingPoint( );
          trackBarTabIndex = ;
          trackBarSize = new SystemDrawingSize( );
          trackBarScroll += new SystemEventHandler(thistrackBar_Scroll);
          trackBarEnabled = false;
          thisControlsAdd(thistrackBar);
       }

    單擊Retrieve按鈕後事件處理程序就會從Products表中選擇所有的記錄並把它們保存到私有數據集ds中
       protected void retrieveButton_Click(object sender SystemEventArgs e)
       {
          retrieveButtonEnabled = false ;
          ds = CreateDataSet();

    接著綁定兩個文本控件
          textNameDataBindingsAdd(Text ds
                                  ProductsProductName);
          textQuanDataBindingsAdd(Text ds
                                  ProductsQuantityPerUnit);
          trackBarMinimum = ;
          trackBarMaximum = thisBindingContext[dsProducts]Count ;
          textNameEnabled = true;
          textQuanEnabled = true;
          trackBarEnabled = true;
       }

    這裡有一個記錄滾動機制該機制響應TrackBar小圖標的移動
       protected void trackBar_Scroll(object sender SystemEventArgs e)
       {
          thisBindingContext[dsProducts]Position = trackBarValue;
       }
       private DataSet CreateDataSet()
       {
          string source = server=(local)\\NetSDK; +
                          uid=QSUser;pwd=QSPassword; +
                          database=northwind;
          string customers = SELECT * FROM Products;
          SqlConnection con = new SqlConnection(source);
          SqlDataAdapter da = new SqlDataAdapter(customers con);
          DataSet ds = new DataSet();
          daFill(ds Products);
          return ds;
       }
       static void Main()
       {
          ApplicationRun(new ScrollingDataBinding());
       }
    }

    在開始檢索數據時跟蹤欄的最大位置就設置為記錄的個數接著在上面的滾動方法中把Products數據表中BindingContext的位置設置為滾動條的位置這樣就可以有效地改變DataTable中的當前記錄綁定到當前行上的所有控件(在本例中是兩個文本框)就會被更新

    本節介紹了如何綁定到各種數據源上例如數組數據表數據視圖和各種其他數據容器如何排序和過濾數據節將討論如何擴展Visual Studio以允許進行數據訪問得到與應用程序的更好集成

[]  []  []  []  


From:http://tw.wingwit.com/Article/program/net/201311/15550.html
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.