C# 检查DataGridView中的控件
我需要的是,如果DataGridView中没有选中复选框,则禁用按钮Uno和按钮Varios。 如果选中单个复选框,则启用按钮Uno,禁用按钮Varios。 如果选中了多个复选框,则禁用按钮Uno,并启用按钮Varios 但是,发生了以下情况: 我使用的代码如下所示:C# 检查DataGridView中的控件,c#,winforms,datagridview,C#,Winforms,Datagridview,我需要的是,如果DataGridView中没有选中复选框,则禁用按钮Uno和按钮Varios。 如果选中单个复选框,则启用按钮Uno,禁用按钮Varios。 如果选中了多个复选框,则禁用按钮Uno,并启用按钮Varios 但是,发生了以下情况: 我使用的代码如下所示: public Form1() { InitializeComponent(); btnUno.Enabled = false; btnVarios.Enabled = false; } 要启用和禁用按钮
public Form1()
{
InitializeComponent();
btnUno.Enabled = false;
btnVarios.Enabled = false;
}
要启用和禁用按钮,请执行以下操作:
private void dtgTitulo_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int contador = 0;
foreach (DataGridViewRow row in dtgTitulo.Rows)
{
if (row.Cells["Seleccione"].Value != null && row.Cells["Seleccione"].Value.Equals(true))//Columna de checks
{
contador++;
if (contador <= 0)
{
btnUno.Enabled = false;
btnVarios.Enabled = false;
}
else if (contador == 1)
{
btnUno.Enabled = true;
btnVarios.Enabled = false;
}
else
{
btnUno.Enabled = false;
btnVarios.Enabled = true;
}
}
}
}
一、表格负荷:
private void Form1_Load(object sender, EventArgs e)
{
ds = new DataSet();
ds.Tables.Add(Query());
ds.Tables[0].Columns.Add("Seleccione", typeof(bool));
dtgTitulo.DataSource = ds.Tables[0];
}
请尝试以下代码:
根据@JIMI的建议
private void dtgTitulo_CellMouseUp(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex != 1) return;
dtgTitulo.CommitEdit(DataGridViewDataErrorContexts.Commit);
var contador = dtgTitulo.Rows.OfType<DataGridViewRow>().Count(r => (r.Cells[1].Value != null) && ((bool)r.Cells[1].Value == true));
if (contador <= 0)
{
btnUno.Enabled = false;
btnVarios.Enabled = false;
}
else
{
if (contador == 1)
{
btnUno.Enabled = true;
btnVarios.Enabled = false;
}
else
{
btnUno.Enabled = false;
btnVarios.Enabled = true;
}
}
}
单击复选框后,它们会显示/隐藏记号,但单元格中的值不会立即更改。调用EndEdit以应用它们
private void dtgTitulo_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex < 0 || dtgTitulo.Columns[e.ColumnIndex].Name != "Seleccione")
return;
dtgTitulo.EndEdit();
int contador = 0;
foreach (DataGridViewRow row in dtgTitulo.Rows)
{
if (Equals(true, row.Cells["Seleccione"].Value))
{
contador++;
if (contador > 1)
break;
}
}
btnUno.Enabled = contador == 1;
btnVarios.Enabled = contador > 1;
}
p、 注意:为避免不必要的迭代而进行的优化尝试在foreach循环无效后编写启用-禁用代码,与我在gif中显示的错误相同@Lucifer@FrancoJoelBalsamo请参阅上面的代码将事件更改为单元格值已更改,并确保启用/禁用代码在CellMouseUp中正确,添加以下内容:如果e.ColumnIndex!=1返回;dtgTitulo.CommittedITDataGridViewDataErrorContexts.Commit;var cellCount=dtgTitulo.Rows.OfType.Countr=>r.Cells[1]。值!=null&&boolr.Cells[1]。值==true;Cell.Value仅在将所选内容移动到另一个单元格时才会更改,因此您可能希望在计数之前提交更改。System.InvalidCastException:“指定的转换无效。”在此行中:r.Cells[1]。Value!=null&&boolr.Cells[1]。值==true。ToString@FrancoJoel Balsamo你为什么要这么做。在那里玩游戏?你使用CellContentClick事件吗?没错,我是在cellvaluechanged上玩的。谢谢你的回答
private void dtgTitulo_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex < 0 || dtgTitulo.Columns[e.ColumnIndex].Name != "Seleccione")
return;
dtgTitulo.EndEdit();
int contador = 0;
foreach (DataGridViewRow row in dtgTitulo.Rows)
{
if (Equals(true, row.Cells["Seleccione"].Value))
{
contador++;
if (contador > 1)
break;
}
}
btnUno.Enabled = contador == 1;
btnVarios.Enabled = contador > 1;
}