[英]How can I use an unbound field in a DataGridView?
我有一個工作DataGridView
有兩個TextBoxColumn
S和DataGridViewButtonColumn
。
作為數據源,我將DataTable
綁定了三列: “ ID” , “ Name”和“ Surname” ,但是我只需要顯示最后兩列,因此在我的數據網格中,我僅定義了Name
和Surname
(以及button列)設置AutoGenerateColumns
屬性等於false
。
現在,當單擊行上的按鈕時,我需要使用“ ID”字段...
從當前行獲取“ ID”值的正確方法是什么?
如果要顯示其他列,我將保留ID列,然后通過將Columns["ID"].Visible
設置為隱藏Columns["ID"].Visible
。 然后,您可以只訪問列中的數據,但它不會顯示在屏幕上。
gridview.Rows[gridview.CurrentRow.Index].Cells["id"].Value
您將獲得當前行的“ id”值
隱藏某些東西。 .Visible
不能很快起作用。 嘗試使用DataView
和BindingSource
。
看到這個鏈接 。
有些filters
效果很好。 像這樣:
BindingSource tSource = new BindingSource();
DataView view = new DataView(_table);
tSource.DataSource = view;
在這里我找到一行:
DataRow row = CurrentRow(_dataGridView);
這是實現:
public static DataRow CurrentRow(DataGridView aGrid)
{
CurrencyManager xCM =
(CurrencyManager)aGrid.BindingContext[aGrid.DataSource, aGrid.DataMember];
DataRowView xDRV = (DataRowView)xCM.Current;
return xDRV.Row;
}
這樣的事情應該起作用。
希望這對您有所幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.