2011-02-26 57 views
1

嗨,如果我綁定數據表格的結果到一個gridview,我如何從該gridview獲取databounditem?下面的代碼失敗,因爲它不能轉換爲對象。將對象轉換爲數據綁定值

private void gvInv_MouseUp(object sender, MouseEventArgs e) 
    { 
     if (null == gvInv.CurrentRow) return; 
     inventory = (Inventory)gvInv.CurrentRow.DataBoundItem; 

請指教。

+0

什麼的DataBoundItem。聲稱在調試器中? – 2011-02-27 00:02:48

+0

當我選擇dataBoundItem時,我希望它返回該對象中的所有屬性 - 庫存。 – VeecoTech 2011-02-27 06:28:25

+0

是的,但是當您在調試器中查看'gvInv.CurrentRow.DataBoundItem'時,通過'gvInv_MouseUp'中的斷點,**它是什麼**?這是關鍵... – 2011-02-27 09:22:34

回答

2

從意見,DataBoundItemDataRowView,所以我假設你已經綁定到類型化數據集,並Invoice是你的專業DataRow

在這種情況下,你要的對象是行視圖的.Row,即

var rowView = gvInv.CurrentRow.DataBoundItem as DataRowView; 
if(rowView != null) 
{ 
    inventory = rowView.Row as Inventory; 
} 
+0

@Marc Gravell:我得到了這個錯誤'rowView.Row作爲庫存「錯誤無法通過引用轉換,裝箱轉換,拆箱轉換,包裝轉換或空類型轉換將類型」System.Data.DataRow「轉換爲」le.DataModel.Inventory「\t 」 – VeecoTech 2011-03-01 12:30:05

+0

@belinq - 好吧,**是'庫存'一個類型化的DataRow,還是完全不相關? – 2011-03-01 12:31:40

+0

@Marc Gravell:Inventory是一個類對象。我在DataModel上寫了一個Class調用Inventory.cs,調用庫存 – VeecoTech 2011-03-01 12:36:34