如何使用 C# 清除 DataGridView 中的行? [英] How can I clear rows in DataGridView with C#?

查看:108
本文介绍了如何使用 C# 清除 DataGridView 中的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此行出现以下错误.

datagridview1.Rows.Clear()

但是这一行给出了错误:

but this line gives error:

无法清除此列表.

推荐答案

当我将 DataGridView 绑定到 DataTable 后尝试清除 DataSource 时,我也遇到了类似的问题:

I also had similiar problem when I try to clear DataSource of a DataGridView after I had binded it to a DataTable with:

DataTable DT = ... ; // fill it
datagridview1.DataSource = DT;

当我稍后尝试使用以下代码清除它的行时:

when I try to clear it's rows later with the following code:

datagridview1.DataSource = null

我收到此错误:

ERROR: Object reference not set to an instance of an object

这个对象引用ERROR问题已经在这里.但我设法用以下代码解决了我自己的问题(清除 DataGridView 行):

This object reference ERROR problem already solved in here. But I managed to solve my own problem (clearing DataGridView rows) with this code:

DataTable DT = (DataTable)datagridview1.DataSource;
if (DT != null) 
    DT.Clear();

我知道这个问题很久以前就有人问过了,但我希望我的解决方案能解决一些人的问题.

I know that this question was asked a very long time ago, but I hope my solution would solve someone problems.

这篇关于如何使用 C# 清除 DataGridView 中的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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