gpt4 book ai didi

c# - DataGridView 按钮列在第一次加载时没有正确加载

转载 作者:太空宇宙 更新时间:2023-11-03 14:39:52 26 4
gpt4 key购买 nike

我有一个加载 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。它正确地禁用了按钮。为什么在第一次加载时不禁用它?那没有任何意义。问候

最佳答案

网格需要在屏幕上可见才能执行此类操作,因此请尝试将 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;
}
}
}));
}

关于c# - DataGridView 按钮列在第一次加载时没有正确加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57632185/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com