DataGridView按钮列在首次加载时未正确加载

时间:2019-08-23 19:43:44

标签: c# winforms datagridview

我有一种将DataGridView加载为的方法:

private void LoadgvList()
        {
                this.dgCustomerList.DataSource = db.GetTableBySQL(query);

//Create column

 var dgvBidButCol = new DataGridViewDisableButtonColumn()
                        {
                            Name = "btnCustomerCreateAgreement",
                            Text = "Generate",
                            HeaderText = "Agreement",
                            Width = 70,
                            UseColumnTextForButtonValue = true,
                        };
                        this.dgCustomerList.Columns.Add(dgvBidButCol);

//Disable or enable button depending of received values

     foreach (DataGridViewRow row in dgCustomerList.Rows)
                        {
                            var canHavePricingRowCells = row.Cells["CanHavePricing"].Value is null ? false : Convert.ToBoolean(row.Cells["CanHavePricing"].Value);
                            var currentBidToValue = ((DataGridViewDisableButtonCell)row.Cells["Bid To"]).FormattedValue.ToString();

                            if (canHavePricingRowCells is false || currentBidToValue == "No")
                            {
                                ((DataGridViewDisableButtonCell)row.Cells["btnCustomerCreateAgreement"]).Enabled = false;
                            }
                        }
            }

在第一次加载时,当我再次通过调用方法刷新它时,每个按钮都为Enabled = true。它正确禁用了按钮。为什么在首次加载时未禁用它?那没有任何意义。问候

1 个答案:

答案 0 :(得分:0)

网格必须在屏幕上可见才能执行此类操作,因此请尝试将TabControl的Selected事件与BeginInvoke结合使用以强制进行视觉更新:

void tabControl1_Selected(object sender, TabControlEventArgs e) {
  this.BeginInvoke(new Action(() => {
    foreach (DataGridViewRow row in dgCustomerList.Rows)
    {
      var canHavePricingRowCells = row.Cells["CanHavePricing"].Value is null ? false : Convert.ToBoolean(row.Cells["CanHavePricing"].Value);
      var currentBidToValue = ((DataGridViewDisableButtonCell)row.Cells["Bid To"]).FormattedValue.ToString();
      if (canHavePricingRowCells is false || currentBidToValue == "No")
      {
        ((DataGridViewDisableButtonCell)row.Cells["btnCustomerCreateAgreement"]).Enabled = false;
      }
    }
  }));
}