如何在DataGridView中使用未绑定字段
本文关键字:绑定 字段 DataGridView | 更新日期: 2023-09-27 18:20:42
我正在使用一个具有两个TextBoxColumn
和一个DataGridViewButtonColumn
的DataGridView
。
作为数据源,我绑定了一个 现在,当单击行上的按钮时,我需要使用"ID"字段。。。 从当前行获取"ID"值的正确方法是什么?DataTable
,它有三列:"ID"、"Name"、"Surname"
如果要显示其他列,我会保留ID列,并通过将Columns["ID"].Visible
设置为false来隐藏它。然后您可以访问列中的数据,但它不会显示在屏幕上。
要隐藏.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;
}
这样的东西应该行得通。
希望这能帮助你!
gridview.Rows[gridview.CurrentRow.Index].Cells["id"].Value
您将获得当前行的"id"值