[英]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.