32

我有一个DataGridView我设置的地方DataSource

taskerEntities te = new taskerEntities();
var OMsMasterDescriptiveIndicators = te.MyTable.Select(x => new lccls {Id = x.Id, name = x.name }).ToList();
MyGrid.DataSource = OMsMasterDescriptiveIndicators;

和我class lccls一样

public class lccls
    {
        public string Id { get; set; }
        public Nullable<decimal> name { get; set; }
    }

在某个事件中,我想让当前行不可见:

 MyGrid.Rows[5].Visible = false;

但我无法做到这一点。相反,会引发异常并显示以下错误消息:

与货币经理职位相关的行不能隐藏

我怀疑原因与设置有关DataSource,但为什么呢?

4

7 回答 7

64

经过大量搜索,我得到了解决方案

CurrencyManager currencyManager1 = (CurrencyManager)BindingContext[MyGrid.DataSource];  
currencyManager1.SuspendBinding();
MyGrid.Rows[5].Visible = false;
currencyManager1.ResumeBinding();
于 2013-09-22T09:41:46.767 回答
15

当前行索引时无法将 yourDataGridView 行可见属性设置为 false 尝试隐藏当前单元格时会遇到此类错误

解决方案:

当 yourDataGridView 数据源不为 null 时:

  CurrencyManager currencyManager1 = (CurrencyManager)BindingContext[yourDataGridView.DataSource];
                       currencyManager1.SuspendBinding();
                       yourDataGridView.Rows[Target Index].Visible = false;
                       currencyManager1.ResumeBinding();

当 yourDataGridView 数据源为 null 时:

 yourDataGridView.CurrentCell = null;
 yourDataGridView.Rows[Target Index].Visible = false;
于 2016-02-04T04:37:52.687 回答
2

例子

        foreach (DataGridViewRow rw in dataGridView1.Rows)
        {

        if (rw.Cells[14].Value.ToString() == "") // this Cell have a TEXT, 
            {
                CurrencyManager currencyManager1 = (CurrencyManager)BindingContext[dataGridView1.DataSource];
                currencyManager1.SuspendBinding();
                rw.Visible = false; 
                currencyManager1.ResumeBinding();

            }
        }

这仅显示单元格索引 14 中的行,如果为空白或为空,则不显示整行

于 2019-01-02T21:19:35.810 回答
1

我有一个 U 示例。我有一个可以多选行的 datagridview。当我单击按钮以显示所选的假行时。尝试这个:

foreach (DataGridViewRow row in dataGridView1.SelectedRows)
        {
            CurrencyManager currencyManager1 =(CurrencyManager)BindingContext[dataGridView1.DataSource];
                currencyManager1.SuspendBinding();
                dataGridView1.CurrentCell = null;
                row.Visible = false;
        }
        dataGridView1.Refresh();

记得设置属性 SelectionMode: FullRowSelect

于 2013-12-14T07:47:43.193 回答
0

我试图在 CellFormating 事件中隐藏一行,但没有成功。看起来货币管理器不能为引发事件的行暂停,而不是我处理之前的行(第 0 行处理最后一行)

Private Sub DgView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DgView1.CellFormatting
        If DgView1.Rows(e.RowIndex).Cells(e.ColumnIndex).OwningColumn.Name = CoID Then
            Dim k = If(e.RowIndex = 0, DgView1.RowCount - 1, e.RowIndex - 1)
            DgView1.Rows(k).Visible = Countries.Rows(k)("Ro")
        End If
End Sub
于 2021-05-18T08:58:21.087 回答
0

回答这个话题可能有点晚了,但我建议您使用 DataTable.DefaultView.RowFilter 属性来过滤您需要在有界 DataGridView 上显示的内容。请查看以下链接以获取更多信息: https ://docs.microsoft.com/en-us/dotnet/api/system.data.dataview.rowfilter?redirectedfrom=MSDN&view=netframework-4.8#System_Data_DataView_RowFilter

问候。

于 2019-09-26T07:27:21.310 回答
0

我知道这是一个老话题,但另一种解决方案(我的代码是 vb.net,但我认为它会翻译)

If WO_DGV.CurrentCell.RowIndex = i Then
  'you cannot make invisible the row that is 'current'
  WO_DGV.CurrentCell = WO_DGV.Rows(i - 1).Cells("act") 
  'to get to this code I know that there is a row before i, which is why I can use i-1 as new focus
End If
WO_DGV.Rows(i).Visible = False 
于 2020-06-24T19:17:44.953 回答