更新DatagridView记录 [英] Updating DatagridView Records

查看:90
本文介绍了更新DatagridView记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做什么错?我似乎可以理解此错误对象引用未设置为对象的实例".请帮助我,谢谢!

 da2 = 新建 OleDbDataAdapter(" ,conDB)
        da2.Fill(ds2," )
        对于 i =  0   poDG. .Count- 1 
            ds2.Tables(" ).Rows(inc).Item(  ItemNo")= poDG.Rows.Item(i).Cells(" ).Rows(inc).Item( 描述")= poDG.Rows.Item(i).Cells(" ).Rows(inc).Item( 数量")= poDG.Rows.Item(i).Cells(" ).Rows(inc).Item(  UnitPrice")= poDG.Rows.Item(i).Cells(" ).Rows(inc).Item( 金额")= poDG.Rows.Item(i).Cells( Dim  cb2  As  新建 OleDbCommandBuilder(da2)
            da2.Update(ds2," )
        下一步 

解决方案

您说的是这行出错的代码:

 ds2.Tables(" ).Rows(inc). Item(" )= poDG.Rows.Item(i).Cells( 0 ).Value.ToString 



因此,您需要确定未将哪个部分设置为实例,或者换句话说,哪个部分为空或什么都不做.您可以通过设置一个断点并运行代码来做到这一点.然后使用QuickWatch. (突出显示要查看的部分,右键单击,选择快速监视).首先确保ds2存在.然后,确保其中确实有一个名为PurchaseDB2的表.然后确保该表内有行.等

我注意到您正在尝试设置row(inc),但是我看不到inc在什么位置.您的意思是只在这里使用变量i吗?还是您要设置为等于的DataGridView对象没有任何行,并且是.Cells(0)部分未设置为对象?

另外,您可能要考虑使用For Each遍历表或网格.看来您正在遍历datagridview,在这种情况下,您可以使用如下所示的方法:

 对于 每个 dgvr  poDG.Rows中的 DataGridView行
  ' 现在,当您要引用DataGridViewRow时,可以使用dgvr.Item(0).Value代替poDG.Rows( i).Cells(0).Value 

下一步 


或遍历一个表:

 对于 每个 myRow  dtMyDataTable.Rows中的DataRow 
   ' 使用myRow而不是dtMyDataTable.Rows(i).Item(0)的代码

下一步 



它比设置那些计数变量要容易一些.希望这会有所帮助.


这是代码的场景,我将在datagridview中输入数据,然后如果单击保存",它将被保存在数据库中,添加现在可以正常工作.我的问题是,在数据库中搜索记录,例如,我搜索了一条记录的描述",因此我从datagridview中添加到数据库中的所有记录都应该仍然在datagridview中.但这没有发生,它只会输出数据库中的第一条记录,如果我希望在datagridview中出现多行怎么办?


您访问数据库的方法是什么? /blockquote>

What am i doing wrong? I can seem to understand this error "Object reference not set to an instance of an object." Pls help me, thanks!

da2 = New OleDbDataAdapter("SELECT * FROM PurchaseDB2", conDB)
        da2.Fill(ds2, "PurchaseDB2")
        For i = 0 To poDG.Rows.Count - 1
            ds2.Tables("PurchaseDB2").Rows(inc).Item("ItemNo") = poDG.Rows.Item(i).Cells(0).Value.ToString
            ds2.Tables("PurchaseDB2").Rows(inc).Item("Description") = poDG.Rows.Item(i).Cells(1).Value.ToString
            ds2.Tables("PurchaseDB2").Rows(inc).Item("Qty") = poDG.Rows.Item(i).Cells(2).Value.ToString
            ds2.Tables("PurchaseDB2").Rows(inc).Item("UnitPrice") = poDG.Rows.Item(i).Cells(3).Value.ToString
            ds2.Tables("PurchaseDB2").Rows(inc).Item("Amount") = poDG.Rows.Item(i).Cells(4).Value.ToString
            Dim cb2 As New OleDbCommandBuilder(da2)
            da2.Update(ds2, "PurchaseDB2")
        Next

解决方案

You say this is the line of code that errors:

ds2.Tables("PurchaseDB2").Rows(inc).Item("ItemNo") = poDG.Rows.Item(i).Cells(0).Value.ToString



So you need to figure out which part is not set to an instance, or in other words, which part is null or nothing. You can do this by setting a break point and running the code. Then use QuickWatch. (highlight the part you want to look into, right click, select quickwatch). First make sure that ds2 exists. Then make sure there is really a table inside it named PurchaseDB2. Then make sure that there are rows inside that table. etc.

I noticed that you are trying to set row(inc), but I don''t see what/where inc is. Did you mean to just use the variable i here? Or perhaps the DataGridView object that you are setting it equal to doesn''t have any rows and it''s the .Cells(0) part that is not set to an object?

Also, you may want to consider using a For Each to iterate through tables or grids. It appears you are iterating through a datagridview in which case you could use something like this:

For Each dgvr as DataGridView Row in poDG.Rows
  'Now when you want to reference the DataGridViewRow you can use dgvr.Item(0).Value instead of poDG.Rows(i).Cells(0).Value

Next


or looping through a table:

For Each myRow as DataRow in dtMyDataTable.Rows
   'Code that uses myRow instead of dtMyDataTable.Rows(i).Item(0)

Next



It''s just a bit easier to use than setting up those count variables. Hope this helps.


here is the scenario of the code, i will input data in the datagridview, then if i click save, it will be saved in the database, adding is now working properly. My problem is, searching the database for records, for example i searched the "Description" of a record so all the records i added in database from datagridview should "be" still there in the datagridview. But this is not happening, it will just output the first record in the database, what if i want multiple rows to appear in the datagridview?


What is your method of accessing the database?


这篇关于更新DatagridView记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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