【发布时间】:2012-01-29 09:59:40
【问题描述】:
嗨,我有一个 datagridview,其第一列(索引 = 0)是一个复选框列。我想在选中和取消选中单元格时更新标签。我的函数似乎工作正常,但在更改该列中的下一个单元格之前它不会更新标签。
当表单加载时,所有行都被选中,所以说 4 of 4. 我取消选中一行并且标签不会更新。我取消选中另一行,然后它显示 3,所以你看到它落后了一步。
我尝试了一些不同的 DataGridViewCellEvents,例如 CellValueChanged、CellStateChanged、CellEndEdit,但它们都按照自己的方式行事。我仍然需要有一个 DataGridViewCellEventArgs e 以便我可以检查该列。
有什么建议吗?
private void fileGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
{
int numberOfFiles = 0;
for (int i = 0; i < fileGrid.Rows.Count; i++)
{
Console.WriteLine(fileGrid[0, i].Value.ToString());
if (fileGrid[0, i].Value.ToString() == "True")
{
numberOfFiles++;
}
}
numberOfFilesLabel.Text = numberOfFiles.ToString();
}
}
我还不能回答我自己的问题,但这是我以前完成的:
private void fileGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
{
int numberOfFiles = 0;
for (int i = 0; i < fileGrid.Rows.Count; i++)
{
Console.WriteLine(fileGrid[0, i].Value.ToString());
if (fileGrid[0, i].Value.ToString() == "True")
{
numberOfFiles++;
}
}
if (fileGrid.IsCurrentCellDirty)
fileGrid.CommitEdit(DataGridViewDataErrorContexts.Commit);
if (fileGrid.CurrentCell.Value.ToString().Equals("True"))
{
numberOfFiles++;
}
if (fileGrid.CurrentCell.Value.ToString().Equals("False"))
{
numberOfFiles--;
}
numberOfFilesLabel.Text = numberOfFiles.ToString();
}
}
【问题讨论】:
标签: c# events datagridview cell