如何在DataGridView中使用未绑定字段

本文关键字:绑定 字段 DataGridView | 更新日期: 2023-09-27 18:20:42

我正在使用一个具有两个TextBoxColumn和一个DataGridViewButtonColumnDataGridView

作为数据源,我绑定了一个DataTable,它有三列:"ID"、"Name"、"Surname"

现在,当单击行上的按钮时,我需要使用"ID"字段。。。

从当前行获取"ID"值的正确方法是什么?

如何在DataGridView中使用未绑定字段

如果要显示其他列,我会保留ID列,并通过将Columns["ID"].Visible设置为false来隐藏它。然后您可以访问列中的数据,但它不会显示在屏幕上。

要隐藏.Visible无法快速执行的内容。尝试使用DataViewBindingSource

请参阅此链接。

有一些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"值