删除datagridview中的行 [英] deleting rows in a datagridview

查看:96
本文介绍了删除datagridview中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DGV,用于存放订单上的物品。这些项目由用户输入

,并且不受任何源表的约束。我可以在DGV中添加/更改项目行。我可以删除行

并且DGV相应地调整自己。

示例:我在DGV中有七行,行编号为1-7

我删除第二行

该行消失。这些行现在编号为1-6

如果我尝试删除现在的第2行,则会出现异常错误

说明我无法访问已删除的行。

关于如何最好地解决这个问题的任何想法。

I have a DGV that houses items to be place on a order. The items were entered by the user
and are not bound to any source table. I can add/change items rows in the DGV. I can delete rows
and the DGV adjusts itself accordingly.
Example: I have seven rows in the DGV the rows are numbered 1-7
I delete row 2
The row disappears. The rows are now numbered 1-6
If I attempt to delete what is now row 2 it gets an exception error
stating I can not access a deleted row.
Any ideas on how to best resolve this problem.

推荐答案

你好,



我创建了一个小应用程序,它包含一个带有以下控件的表单:



1)2个按钮:

-Add:在datagridview中添加数据。

-Delete:从datagridview中删除数据。



2)DataGridView:

-datagridview1



3)2个文本框:

- textbox1:通过此文本框将数据输入datagridview。

- textbox2:指定要删除数据的索引。因此,请输入要删除数据的索引。



Hello ,

I have created a small application it contains a form with the following controls:

1)2 buttons:
-Add: Adds the data in datagridview.
-Delete: Delete data from the datagridview.

2)DataGridView:
-datagridview1

3)2 textboxes:
- textbox1: enter data into the datagridview through this textbox.
- textbox2: this specifies the index at which you want to remove the data. So enter the index at which you want to remove the data.

public partial class Form1 : Form
  {
      public Form1()
      {
          InitializeComponent();
          dataGridView1.Columns.Add("col1", "data");////Initialize the columns and give it a name.
          dataGridView1.RowsRemoved += new DataGridViewRowsRemovedEventHandler(removed);///When you remove an item this event handler fires


      }

      private void add_Click(object sender, EventArgs e)
      {
          dataGridView1.Rows.Add(textBox1.Text);///fetch an item from textbox and add it to the datagridview
       }

      private void delete_Click(object sender, EventArgs e)
      {
          dataGridView1.Rows.RemoveAt(int.Parse(textBox2.Text));////Remove At the location specified in the textbox
      }

      public void removed(object sender, DataGridViewRowsRemovedEventArgs e)
      {

          MessageBox.Show("Removed  ");///pops up when you delete an item
      }

  }





- 请记住,假设您在datagridview中有3个元素。要删除3元素,您必须指定索引:2,因为它是基于零的索引,从0开始而不是1.因此您可能得到的异常是:



- Please remember suppose you have 3 elements in the datagridview. To delete the 3 element you will have to specify the index : 2, because it is a zero based index, starts from 0 not 1. So the exception you might have got is :

引用:

无法删除未提交的新行。

Uncommitted new row cannot be deleted.

(如果我错了,请更正我)。



- 试试吧上面的代码。希望能帮助到你。此代码在按下按钮时添加和删除项目。请告诉我发生了什么。



再次感谢,

-Rahul

(correct me if i am wrong).

- Just try the above code. Hope it helps. This code adds and deletes an item on button press. Please do let me know what happens.

thanks again,
-Rahul


这篇关于删除datagridview中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆