是否有直接的方法來做到這一點,或者是否需要遍歷DataGridView.SelectedRows,然後訪問每行的DataGridViewRow.DataBoundItem以創建一個?獲取與選定的DataGridView行相關的所有DataRows的集合
1
A
回答
3
DataGridView不提供SelectedDataRows和SelectedRows,而不是啓用Linq,所以是的,你將不得不編寫一個foreach循環。
0
的通用擴展方法「SelectedDataRows」添加到DataGridViews:
public static T[] SelectedDataRows<T>(this DataGridView dg) where T : DataRow
{
T[] rows = new T[dg.SelectedRows.Count];
for (int i = 0; i < dg.SelectedRows.Count; i++)
rows[i] = (T)((DataRowView)dg.SelectedRows[i].DataBoundItem).Row;
return rows;
}
這是通用的,這樣就可以使用返回類型化數據集一個類型化的數據行。如果你願意,你可以讓你的返回DataRow[]
。這也假設你的DataGridView綁定了一個DataView
。
相關問題
- 1. 獲取對象與DataGridView中的選定行關聯時出錯
- 2. 獲取與DataGridView中選定行關聯的對象
- 3. 獲取的DataGridView的集合頁眉
- 4. 從DataGridView獲取選定的行
- 5. 如何獲取與其他符號相關的所有定義?
- 6. 獲取與用戶相關的所有行
- 7. 獲取與空集相關的那些
- 8. 獲取與集合對象相關的表
- 9. 獲取所有的IDS從集合
- 10. ActiveRecord的 - 獲取所有相關記錄
- 11. CoreData獲取所有相關的對象
- 12. 如何選擇所有相關的行?
- 13. 獲取所選列數出來的DataGridView
- 14. laravel如何獲得與集合的所有嵌套關係
- 15. 綁定到DataGridView的集合
- 16. 獲取java.lang包中所有類的集合(或集合)
- 17. 從datagridview的選擇中獲取數據表中相應的行
- 18. 如何將DataRows數組綁定到DataGridView
- 19. 如何獲取datagridview組合框的選定項的值
- 20. 如何獲取與內容相關的所有標籤?
- 21. HashMap - 獲取與鍵值相關的所有值
- 22. 獲取與控制器相關的所有視圖 - Zend
- 23. 獲取與郵編相關的所有城市
- 24. 獲取與角色相關的所有Sentinel用戶
- 25. 檢索所有相關數據符合指定標準的行
- 26. 所有Datagridview中的一行選擇器
- 27. 如何選擇DataGridView中的所有行?
- 28. SQL獲取相關的列集合的結果,
- 29. 獲取不同的用戶,但所有其他行相關聯
- 30. 獲取選定的dataGridView列數據?
這聽起來很直接。 – mquander 2009-12-01 21:49:45
假設的DataGridView.GetSelectedRowsInDataSource方法比我自己編寫方法更直接。最好問一個愚蠢的問題,而不是讓維護程序員質疑我重寫框架的能力。 – 2009-12-01 22:06:58