RowFilter之后的DataGridView错误 - >数据更新/添加和填充过程
我有一个由SQL表填充的DataGridView。当我在数据中使用文本框搜索一个值并更新/插入某些值并再次使用填充函数,我会得到此错误 “ system.nullReferenceException hresult = 0x80004003消息=对象引用未设置为对象的实例。”
注意:在不使用搜索文本框的情况下,更新,插入和填充功能没有问题,
这是我的填充功能;
DataTable dt1 = new DataTable();
void fill()
{
try
{
//txtSearch.Clear();
dt1.Rows.Clear();
dt1.Columns.Clear();
SqlDataAdapter da = new SqlDataAdapter("Select * From Bilgisayar_Zimmet", bgl.baglanti());
da.Fill(dt1);
dataGridView1.DataSource = dt1;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
这是我的搜索代码;
private void txtSearch_TextChanged(object sender, EventArgs e)
{
(dataGridView1.DataSource as DataTable).DefaultView.RowFilter = String.Format("Name LIKE '%{0}%' OR Surname LIKE '%{1}%'", txtSearch.Text, txtSearch.Text);
}
我认为我的问题是txtsearch_textchanged方法。它阻止填充功能,因为在DGV中有过滤的行。
I have a datagridview which filled by SQL table. When I search a value with a textbox in the data and update/insert the some values and use fill function again I get this error
"System.NullReferenceException HResult=0x80004003 Message=Object reference not set to an instance of an object."
Note: No problem with update, insert and fill function without using search textbox
This is my fill function;
DataTable dt1 = new DataTable();
void fill()
{
try
{
//txtSearch.Clear();
dt1.Rows.Clear();
dt1.Columns.Clear();
SqlDataAdapter da = new SqlDataAdapter("Select * From Bilgisayar_Zimmet", bgl.baglanti());
da.Fill(dt1);
dataGridView1.DataSource = dt1;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Here is my search code;
private void txtSearch_TextChanged(object sender, EventArgs e)
{
(dataGridView1.DataSource as DataTable).DefaultView.RowFilter = String.Format("Name LIKE '%{0}%' OR Surname LIKE '%{1}%'", txtSearch.Text, txtSearch.Text);
}
I think my problem is txtSearch_TextChanged methods. It blocks fill function because in the dgv there is filtered row.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,如果我将帽子挂在数据表PEG上,我将在网络框架项目中进行以下 - 由于vs中的错误/不完整的支持,因此在NetCore+ Projects中关闭了此过程的大部分。它确实可以作为运行时的核心工作,只是不设计(因此您可以在框架中进行此设计,并将代码导入核心,甚至具有编辑相同文件的核心和NetFW项目。):
我会。 ..
在我的DB中有我的桌子:

制作新的数据集类型的文件:

之类的选择 * select * table where there primenyKeycolumn = @id
在此处而不是选择全部,但是您显然希望在客户端中拥有所有2000行。看看高级,然后检查所有3个框都被打勾了@id @id
选择,然后称其为FillbyId etc
底部出现了大量东西,出现网格,显示工具栏。也添加一个文本框(星号) - 我们将其用于过滤器
双击文本框;您会看到最小代码VS已编写以加载数据。该代码的其余部分位于form.designer.cs文件和dataset.designer.cs文件中,如果您想查看。在我们的形式中,我们只需要此代码:
我们不必重新填充任何东西; v刚刚将单个ID删除了DB
我可以输入过滤器:
那么这是什么伏都教?它如何工作?好..这与您的不同。如果我们在dataset.designer.cs中进行录音,我们可以找到拉和推数据的查询:
就在此处读写查询; tabeadapter包装了数据纳普特并设置了相关查询,datarow状态驱动插入或更新等是触发的。。从dataTable继承,以及具有适当属性的行,而不仅仅是您通过字符串列名访问的数组,
我们只会加载数据:
强烈键入的DataTables与Linq更好地使用
etc.等等。
最后一点:我个人不会下载2000排入客户并将其放在那里。我会下载我需要的时间。您拥有的时间越长,可能会越多,它消耗量就越多,您的服务器磁盘/网络就会越来越多地填充数据量的数据。我了解加载所有行之间的权衡,然后燃烧客户端的CPU来搜索它们而不是服务器的搜索,并且“字符串包含”很难最佳地搜索/索引,但是仍然 - 我会查看全文索引和离开 在服务器上
它们在上面进行查询时 ,请勿将
从Bilgisayarzimmet
- put从bilgisayarzimmet中进行选择 *,其中id = @id
select * select * select * 。然后,完成此操作后,添加另一个查询,select *从bilgisayarzimmet中select * pristname(例如@n)或lastname(例如@n
),并将其称为fillbynamelikein Code中,请这样称呼:
甚至更好。让用户提供通配符,Tohugh人可能习惯于编写
*
SO:当TableAdapter填充时,它将首先清除数据表,因此无需自行清除。要关闭此行为,请设置
tableadapter.clearbeforilfill = false
。这样,您的网格仅显示从数据库中删除的数据,这是一个小的,最新的数据集,而不是在AppAll In中询问所有数据,而是写很多乏味的代码,并使生活变得更好。如果在数据表模式下运行,我几乎只使用这些
So, if I was hanging my hat on the DataTable peg I would do the following in a net framework project - large parts of this process are switched off in netcore+ projects because of bugs/incomplete support in VS. It does work in core as a runtime thing, just not design (so you could do this design in framework, and import the code to core, or even have a core and netfw project that edit the same files..):
I would...
Have my table in my DB:

Make a new DataSet type of file:
select * table where primarykeycolumn = @id
here rather than selecting all, but you apparently want to have all 2000 rows in the client. Have a look in advanced and check that all 3 boxes are ticked@id
then call itFillById
etcA load of stuff appears at the bottom, a grid appears, the toolbar appears. Add a textbox too (the asterisk) - we'll use it for the filter
Double click the textbox; you'll see the minimal code VS has written to load the data. The rest of the code is in the Form.Designer.cs file and the DataSet.Designer.cs file if you want to have a look. In our form we just need this code:
We haven't had to refill anything; VS just got the single IDs out the DB
I can type a filter:
So what is this voodoo? How does it work? Well.. It's not much different to yours. If we go rummaging in DataSet.Designer.cs we can find the queries that pull and push data:
read and write queries right there; the tableadapter wraps a dataadapter and sets up the relevant queries, the datarow state drives whether an insert or update etc is fired.. All the code in your question and more is written by VS and tucked away.. The dataset has strongly typed tables in that inherit from DataTable, and rows that have proper properties rather than just an array you access via string column names
We would just load data:
The strongly typed datatables are nicer to LINQ with
etc..
Final point: I personally wouldn't download 2000 rows into the client and hold them there. I'd download what I needed as and when. The longer you have something the more out of date it's likely to be, the more meory it consumes and the more your server disk/network gets battered serving up volumes of data noone uses. I understand the tradeoff between loading all the rows and then burning the client's CPU to search them rather than the server's, and that "string contains" are hard to optimally search/index, but still - I'd look at full text indexing and leaving them on the server
When you're making your query above, don't put
SELECT * FROM BilgisayarZimmet
- putSELECT * FROM BilgisayarZimmet WHERE ID = @id
and call it FillById. Then when you're done with that one, add another query,SELECT * FROM BilgisayarZimmet WHERE FirstName LIKE @n OR LastName LIKE @n
and call it FillbyNameLikeIn code, call it like this:
Or even better let the user supply the wildcard, tohugh people are perhaps used to writing
*
so:When the TableAdapter fills it will clear the datatable first, so no need to clear it yourself. To turn this behaviour off, set
tableAdapter.ClearBeforeFill = false
. This way your grid shows only the data downlaoded from the DB, and that's a small, more recent dataset, than querying it all at app startAll in, VS write a lot of tedious code,and makes life a lot nicer. I use these nearly exclusively if operating in DataTable mode