添加行后索引出现问题 [英] Problem with index after adding a row

查看:80
本文介绍了添加行后索引出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用数据绑定Datagridview控件和BindingNavigator(w / menustrip)控件在Visual Studio 2008(VB NET)中编写的项目。如果我在Binding Navigator上选择Add New按钮,Datagridview控件将移动到索引为-1的新记录,但不会向数据库添加记录。



当我通过查询Me.TableAdapter.AddRow()向数据库添加记录时,该行将添加到数据库中,并且Datagridview控件将移动到索引为-1的新记录,但不会刷新新行。



使用Datagridview的NewRow会产生更糟糕的结果,所以我禁用了允许用户向Datagridview添加新行。



使用以下代码



I have a project written in Visual Studio 2008 (VB NET) using a data bound Datagridview control and BindingNavigator (w/ menustrip) control. If I select the Add New button on the Binding Navigator the Datagridview control moves to a new record with an index of -1 but does not add a record to the database.

When I add a record to the database via a query Me.TableAdapter.AddRow() the row is added to the database and the Datagridview control moves to a new record with an index of -1 but does not refresh with the new row.

Using the NewRow of the Datagridview produces worse results, so I have disabled allowing users to add new rows to the Datagridview.

With the following code

Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click

   Me.TableAdapter.AddData() 'inserts a new row
   BindingSource.ResetBindings(True)

end sub





它有效(向数据库添加一行(SQL)并更新datagridview,然后我可以修改它。但是,我仍然以该行的负索引结束。添加额外的行会减少我的索引(-2,-3等)。任何想法?



it works (adds a row to the database (SQL) and updates the datagridview which I can then modify. However, I still end up with a negative index for that row. Adding additional rows decreases my index (-2, -3, etc). Any ideas anyone?

推荐答案

尝试 BindingSource.AddNew方法 [ ^ ]。


已解决 - 我发现另一个线程有一个类似的问题。当您添加bindingNavigator时,Visual Studio将自行添加一些代码。通常,当用户单击bindingNavigatorAddItem按钮时,除了您添加的任何代码之外,还会触发此代码,从而为您提供越界记录索引。在InitializeComponent过程中,我找到并禁用该行



Me.BindingNavigator1.AddNewItem = Me.BindingNavigatorAddNewItem



这样做解决了我的问题。
Solved - I found another thread with a similar problem. When you add the bindingNavigator Visual Studio will add some code on its own. Normally, when a user clicks the bindingNavigatorAddItem button this code is fired in addition to any code you add giving you an out-of-bounds record index. In the InitializeComponent procedure I located and disabled the line

Me.BindingNavigator1.AddNewItem = Me.BindingNavigatorAddNewItem

Doing so resolved my problem.


已解决 - 我发现了另一个类似问题的线程。当您添加bindingNavigator时,Visual Studio将自行添加一些代码。通常,当用户单击bindingNavigatorAddItem按钮时,除了您添加的任何代码之外,还会触发此代码,从而为您提供越界记录索引。在InitializeComponent过程中,我找到并禁用该行



Me.BindingNavigator1.AddNewItem = Me.BindingNavigatorAddNewItem



这样做解决了我的问题。
Solved - I found another thread with a similar problem. When you add the bindingNavigator Visual Studio will add some code on its own. Normally, when a user clicks the bindingNavigatorAddItem button this code is fired in addition to any code you add giving you an out-of-bounds record index. In the InitializeComponent procedure I located and disabled the line

Me.BindingNavigator1.AddNewItem = Me.BindingNavigatorAddNewItem

Doing so resolved my problem.


这篇关于添加行后索引出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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