C# DataGridView 捕获已删除的行

发布于 2024-09-05 18:03:37 字数 443 浏览 5 评论 0原文

1.

我正在使用 DataGridView,它绑定到数据集。 还有一个绑定导航器。

当用户通过单击“bindingNavigatorDeleteItem”按钮删除一行时,我试图删除该行。

private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)        
{
        int crow = gridEventType.CurrentCell.RowIndex;

但它会在删除选定行后返回新选定的行。 所以,如果我有两行 0 1 2 我删除 2

只乌鸦是 1,而不是 2。

我在这里遗漏了什么吗?

2.我如何知道datagridview中的某些行是否被修改?

谢谢,

1.

I am using a DataGridView, which is bound to a dataset.
There is a bindingNavigator as well.

when a user deletes a row by clicking "bindingNavigatorDeleteItem" button, I am trying to get the row being deleted.

private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)        
{
        int crow = gridEventType.CurrentCell.RowIndex;

but it returns the newly selected row AFTER deletion of a selected row.
so, If I have two rows
0
1
2
and I delete 2

crow is 1, not 2.

Am I missing something here?

2. How do I know whether certain rows are modified in datagridview?

Thanks,

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

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

发布评论

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

评论(4

独留℉清风醉 2024-09-12 18:03:37

使用 datagridview.UserDeletingRow 事件获取已删除的行对象。

Use the datagridview.UserDeletingRow event to get the deleted row object.

来世叙缘 2024-09-12 18:03:37

您应该使用DataGridViewCellEventArgs

private void bindingNavigatorDeleteItem_Click(object sender, 
                                                  DataGridViewCellEventArgs e) 
{
   int cRow =  gridEventType.Rows[e.RowIndex];

   //delete row from persisted storage

   //rebind data to persisted storage or remove row directly:
   gridEventType.Rows.RemoveAt(cRow);
}

对于第二点,有一个 CellValueChangedEvent。

    private void bindingNavigatorModifiedCell_CellValueChanged(object sender, 
                                                   DataGridViewCellEventArgs e)
    {
         //updated row
         int cRow = e.RowIndex;
    }

You should be using the DataGridViewCellEventArgs.

private void bindingNavigatorDeleteItem_Click(object sender, 
                                                  DataGridViewCellEventArgs e) 
{
   int cRow =  gridEventType.Rows[e.RowIndex];

   //delete row from persisted storage

   //rebind data to persisted storage or remove row directly:
   gridEventType.Rows.RemoveAt(cRow);
}

For your second point, there's a CellValueChangedEvent.

    private void bindingNavigatorModifiedCell_CellValueChanged(object sender, 
                                                   DataGridViewCellEventArgs e)
    {
         //updated row
         int cRow = e.RowIndex;
    }
旧街凉风 2024-09-12 18:03:37

对于我的问题#1。

  • 将 BindingNavigator 的“DeleteItem”属性更改为(无)
  • 创建“DeleteItem_Clicked”的事件处理程序
  • 通过

    获取当前行号

    int cRow = gridEventType.CurrentCell.RowIndex;

  • 从绑定源中删除当前项

    BindingNavigator.BindingSource.RemoveCurrent();

For my Question #1.

  • change "DeleteItem" property of BindingNavigator to (none)
  • create an eventhandler of "DeleteItem_Clicked"
  • get current row number by

    int cRow = gridEventType.CurrentCell.RowIndex;

  • delete the current item from bindingsource

    BindingNavigator.BindingSource.RemoveCurrent();

最美不过初阳 2024-09-12 18:03:37

尝试使用 UserDeletingRow 作为删除行的事件处理程序,在该方法中您可以获得行索引,如下所示:

 private void dataGridView1_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
    {
        int tmpId = (int) e.Row.Index;
    }

Try to use UserDeletingRow as an event handler for deleting a row, and inside this method you can get the row index and as below:

 private void dataGridView1_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
    {
        int tmpId = (int) e.Row.Index;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文