DataGridView 数据更新方法

找了很久才弄好的 记录一个解决方案

  /// <summary>
        /// 刷新页面数据
        /// </summary>
        void ResetViewData()
        {
            this.Invoke((Action)(() => {
                for (int i = 0; i < uiDataGridView1.RowCount; i++)
                {
                    uiDataGridView1.InvalidateRow(i);
                }
            }));
        }
        /// <summary>
        /// 刷新页面数据
        /// </summary>
        void ResetViewData(EquipmentComEntity comEntity)
        {
            this.Invoke((Action)(() => {
                for (int i = 0; i < uiDataGridView1.RowCount; i++)
                {
                    if (((EquipmentComEntity)uiDataGridView1.Rows[i].DataBoundItem)== comEntity)
                    {
                        uiDataGridView1.InvalidateRow(i);
                    }
                }
            }));

        }

主要代码就是

 uiDataGridView1.InvalidateRow(i);
其中文翻译为
  //
        // 摘要:
        //     使 System.Windows.Forms.DataGridView 中的指定行无效,并强制对它进行重新绘制。
        //
        // 参数:
        //   rowIndex:
        //     要使其无效的行的索引。
        //
        // 异常:
        //   T:System.ArgumentOutOfRangeException:
        //     rowIndex 不在 0 到行数减 1 的有效范围内。
        public void InvalidateRow(int rowIndex);

这个办法是目前最好的解决方案

posted @ 2022-01-18 02:13  AnAng  阅读(924)  评论(0编辑  收藏  举报