DataGridView CurrentRow 排除新行

发布于 2024-08-01 23:09:54 字数 157 浏览 8 评论 0原文

我想获取 DataGridView 当前行的索引。 问题是,如果当前行是新行,则 CurrentRow 将设置为不是新行的最后一行。 我无法检查要选择的行,因为如果选择了一行,并不意味着它是当前行,并且不一定选择当前行。

我可以获得新行的索引,但如何知道新行是否是当前行?

I want to get the index of the current row of my DataGridView. The problem is, that if the current row is the new row, CurrentRow is set to the last row that is not the new row. I cannot check for the rows to be selected because if a row is selected that doesn't mean it is the current row and the current row isn't necessarily selected.

I can get the index of the new row, but how can I know whether the new row is the current row?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寒尘 2024-08-08 23:09:54

这个房产价值能解决您的问题吗?

dataGridView1.CurrentCell.RowIndex

Can this property value solve your problem?

dataGridView1.CurrentCell.RowIndex

甚是思念 2024-08-08 23:09:54

我使用 CellEnter 事件将 CurrentCell.RowIndex 保存到表单变量中。 单击另一个按钮会导致此事件以最后一个现有行索引触发,而不是我们需要的新行索引,因此测试焦点以忽略这种情况。

这避免了像使用 CellClick 事件那样检查可能将光标移动到新行的所有可能的按键和鼠标操作。

从 VB 自动转换的代码:

private int ActualCurrentRowIndex;

private void DataGridView1_CellEnter(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
{
    if (DataGridView1.Focused) {
        ActualCurrentRowIndex = DataGridView1.CurrentCell.RowIndex;
    }
}

I use the CellEnter event to save CurrentCell.RowIndex into a form variable. Clicking another button causes this event to fire with the last existing row index instead of the new row index that we need so test for focus to ignore that case.

This avoids having to check for all possible key and mouse actions that might move the cursor to the new row as you do with the CellClick event.

Code auto-converted from VB:

private int ActualCurrentRowIndex;

private void DataGridView1_CellEnter(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
{
    if (DataGridView1.Focused) {
        ActualCurrentRowIndex = DataGridView1.CurrentCell.RowIndex;
    }
}
江南烟雨〆相思醉 2024-08-08 23:09:54

我通过以下代码连接到 CellClick 事件:

private void uiEntries_CellClick(object sender, DataGridViewCellEventArgs e)
{
    Console.WriteLine(e.RowIndex == uiEntries.NewRowIndex);
}

当该条件为真时,用户“选择”新行。 根据该信息,您可能需要实际插入一条新记录(使用 ..Rows.Add() 方法),然后才能实际对其执行任何操作,因为新行实际上并不存在于集合中; 它只是作为占位符来表示新行。

I hooked into the CellClick event via the following code:

private void uiEntries_CellClick(object sender, DataGridViewCellEventArgs e)
{
    Console.WriteLine(e.RowIndex == uiEntries.NewRowIndex);
}

When that condition is true, the user 'selected' the new row. Based upon that information, you may need to actually insert a new record (using the ..Rows.Add() method) before you can actually do anything with it since the new row doesn't actually exist in the collection; it's just there as a placeholder to represent a new row.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文