gpt4 book ai didi

c# - 禁用数据 GridView 中的按钮列

转载 作者:可可西里 更新时间:2023-11-01 08:32:29 25 4
gpt4 key购买 nike

我有一个包含 4 列的数据 GridView ,前两列是组合框列,第三列是文本框列,第四列是按钮列。在表单加载中,我必须禁用数据网格的整个按钮列,然后我应该首先选择三列并在保存后将前三列保存在数据库中,特定行中的按钮列应启用。前三列应通过单击按钮保存在数据库中。请帮我解决这个问题好几天了这是我使用的代码

private void SATAddTemplate_Load(object sender, EventArgs e)
{
foreach (DataGridViewRow row in datagrdADDTEMP.Rows)
{

DataGridViewButtonCell btn = (DataGridViewButtonCell)row.Cells[3];
btn.ReadOnly = true;
}
}
private void btnSaveSettings_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in datagrdADDTEMP.Rows)
{

DataGridViewButtonCell btn = (DataGridViewButtonCell)row.Cells[3];
btn.ReadOnly = false;
}
}

最佳答案

这里有一些关于设置出现在 DataGridViewButtonColumn 中的按钮的 Enabled 属性问题的帮助。

您需要扩展 DataGridViewButtonColumn 以创建您自己的带有可禁用按钮的 DataGridView 列。 This article on MSDN详细说明如何执行此操作。

这篇文章有很多代码,我鼓励你仔细看看,但你真正需要做的就是将文章中的以下类复制并粘贴到你的项目中:
-- DataGridViewDisableButtonColumn
-- DataGridViewDisableButtonCell

完成此操作后,您将能够将 DataGridViewDisableButtonColumn 添加到您的 DataGridView。使用自定义列中公开的公共(public) Enabled 属性来设置每个单元格的 Button 控件的 Enabled 属性。由于您想设置列中所有按钮的 Enabled 属性,您可以编写一个辅助方法来循环遍历 DataGridView 中的所有行并设置 Enabled 属性:

private void SetDGVButtonColumnEnable(bool enabled) {
foreach (DataGridViewRow row in dataGridView1.Rows) {
// Set Enabled property of the fourth column in the DGV.
((DataGridViewDisableButtonCell)row.Cells[3]).Enabled = enabled;
}
dataGridView1.Refresh();
}

关于c# - 禁用数据 GridView 中的按钮列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12525305/

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