字典實體類:DictionaryEntry類

來源:互聯網
上載者:User

DictionaryEntry類是一個字典集合,主要包含的內容是鍵/值對。這種組合方式可以方便地定位元據,其中的“鍵”具備唯一性,類似於資料庫中的“id”,一個id對應一天記錄,而一個鍵只對應一個值。

使用DictionaryEnry類可以方便地設定和檢索資料。雖然被稱為字典集合,但DictionaryEntry並不包含一組資料,而只是一個“鍵/值”對,一般通過“IDictionaryEnumerator”、“IOrderedDictionary”或Hashtable來擷取DictionaryEntry執行個體,這一點需要注意。

文法定義:

DictionaryEntry類的文法定義如下所示,注意其屬於“struct”結構類型。

[SerializableAttribute]

[ComVisibleAttribute(true)]

public struct DictionaryEntry

 

DicctionaryEntry類的構造文法如下所示,其中key表示鍵,value表示值。

public DictionaryEntry (object key,object value)

使用DictionaryEntry類一般不用new關鍵字執行個體化,而是使用“foreach”遍曆的方法,從某一集合擷取DictionaryEntry的執行個體,使用方法如下:

foreach(DictionaryEntry dic in e.Keys)

 

DictionaryEntry的屬性

Key :字典的鍵,必須唯一

Value:字典的值。可通過檢索“鍵”擷取或設定

下面示範如何在程式中使用DictionaryEntry類的這兩個屬性:

Response.Write("這是當前資料表格的鍵列表:<br/>");int i =0;foreach(DictionaryEntry dic in e.Keys){    //遍曆並顯示資料表格所有的鍵  i++;   Response.Write("第"+ i.ToString() + "個鍵為:" +dic.Key.ToString() +",值為:" + dic.Value.ToString());}

 用DictionaryEntry擷取GridView中的資料更新

DictionaryEntry類通常不需要為自己設定資料,而是用來擷取某一個集合中的資料。本例使用DictionaryEntry類執行個體化“IOderedDictionary”中的資料,其中“IOrderedDictionary”包含的是GridView的鍵和值。

在使用GridView更新資料時,需要注意如下三點。

1、被更新的記錄的主鍵:主鍵用來確定更新的資料的唯一性。

2、更新記錄的新資料:用在更新資料庫語句中。

3、更新記錄的舊資料:用來判斷更新的一些條件,比如說資料等於1時不被更新。

通過上述三點,可以知道GridView更新資料的一些原理。在GridView更新事件中,提供三個參數來表示上面的三種資料,分別為“Keys”、“NewValues”、“OldValues”。這些資料都以“IOrderedDictionary”的形式儲存。

本例要實現的功能是目前使用者更新資料後,顯示目前使用者更新的資料,同時顯示資料表格的主鍵,主要完成的是一個提示功能。

在GridView的“Row_Updated”事件中,擷取資料更新的索引值,代碼如下:

protected void GridView1_RowUpdated(object sender,GridViewUpdatedEventArgs e){   foreach(DictionaryEntry mydictionary in e.OldValues)   {      //顯示修改前的資料    Response.Write("修改前的資料——" + mydictionary.Key.ToString() + ":" + mydictionary.Value.ToString());    }   foreach(DictionaryEntry mydictionary in e.NewValues)   {      //顯示修改後的資料    Response.Write("修改後的資料——" + mydictionary.Key.ToString() + ":" + mydictionary.Value.ToString());    }   foreach(DictionaryEntry mydictionary in e.Keys)   {      //顯示當前表格的主鍵——可能有多個,如果沒有則無法更新    Response.Write("<br/>當前主鍵——" + mydictionary.Key.ToString() + ":" + mydictionary.Value.ToString());    }    }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.