从datagrid删除选定的行 [英] delete selected row from datagrid

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

问题描述

你好,

如何从datagridview的选定行和单元格中获取值,然后再将其删除?

这是我的代码

Hello,

How to get a value from selected row and cell from datagridview before deleting it?

This is my code

dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
                string stock_code =dataGridView1.CurrentRow.Cells[0].Value.ToString();
                string q = dataGridView1.CurrentRow.Cells[0].Value.ToString();



出现此错误:
对象引用未设置为对象的实例



gives this error:
Object reference not set to an instance of an object

推荐答案

现在,您的代码已反转.您删除该行,然后尝试读取其值.将删除项放在最后,如果编码正确,则应该获得所需的信息.它应该是这样的:
Right now your code is reversed. You delete the row and then try to read its values. Put the delete last and if you coded it right, you should get the information you want. Here is what it should look like:
string stock_code =dataGridView1.CurrentRow.Cells[0].Value.ToString();
string q = dataGridView1.CurrentRow.Cells[0].Value.ToString();
dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);


该错误表示您使用的对象之一是null.
尝试使用调试器找出哪个对象是null.
The error means that one of the objects that you use is null.
Try to find out with the debugger which object is null.


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

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