[英]How can I use an unbound field in a DataGridView?
I'm working with a DataGridView
having two TextBoxColumn
s and a DataGridViewButtonColumn
. 我有一个工作DataGridView
有两个TextBoxColumn
S和DataGridViewButtonColumn
。
As datasource I bound a DataTable
with three columns: "ID" , "Name" , "Surname" , but I need to show only the last two columns, so in my datagrid I defined only Name
and Surname
(and the button column) setting the AutoGenerateColumns
property equal to false
. 作为数据源,我将DataTable
绑定了三列: “ ID” , “ Name”和“ Surname” ,但是我只需要显示最后两列,因此在我的数据网格中,我仅定义了Name
和Surname
(以及button列)设置AutoGenerateColumns
属性等于false
。
Now I need to use the "ID" field when the button on a row is clicked... 现在,当单击行上的按钮时,我需要使用“ ID”字段...
What's the correct way to get the "ID" value from the current row? 从当前行获取“ ID”值的正确方法是什么?
If it's about showing the other columns, I'd leave the ID column in, and hide it by setting Columns["ID"].Visible
to false. 如果要显示其他列,我将保留ID列,然后通过将Columns["ID"].Visible
设置为隐藏Columns["ID"].Visible
。 Then you can just access the data in the column, but it doesn't show on screen. 然后,您可以只访问列中的数据,但它不会显示在屏幕上。
gridview.Rows[gridview.CurrentRow.Index].Cells["id"].Value
您将获得当前行的“ id”值
To hide something .Visible
do not works fast. 隐藏某些东西。 .Visible
不能很快起作用。 Try to use DataView
and BindingSource
. 尝试使用DataView
和BindingSource
。
There are filters
that works very good. 有些filters
效果很好。 Like this: 像这样:
BindingSource tSource = new BindingSource();
DataView view = new DataView(_table);
tSource.DataSource = view;
Here I find a row: 在这里我找到一行:
DataRow row = CurrentRow(_dataGridView);
Here is the Implementation: 这是实现:
public static DataRow CurrentRow(DataGridView aGrid)
{
CurrencyManager xCM =
(CurrencyManager)aGrid.BindingContext[aGrid.DataSource, aGrid.DataMember];
DataRowView xDRV = (DataRowView)xCM.Current;
return xDRV.Row;
}
Something like this should work. 这样的事情应该起作用。
Hope this help you! 希望这对您有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.