使用绑定在代码中的控件添加记录 [英] Adding record with controls bound in code

查看:47
本文介绍了使用绑定在代码中的控件添加记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Binding Manager&数据集 - 不会添加记录

我有一个无类型的数据集,其中的控件通过代码绑定。用户

可以从绑定的组合框中选择一个问题编号,问题

编号和问题文本显示在绑定的文本框中。这部分

工作正常。

当我去添加新记录时,文本框应该清楚。我

在问题的文本框中输入一个新的问题编号和标签

text。此时,先前选择的问题

(其编号仍显示在组合框中)的文本重新出现。如果我然后

无论如何都要更改文本并尝试保存,它会尝试更新以前选择的记录而不是添加新记录。我认为它有一些与未更改的数据集位置有关的事情,

尽管bindingmanagerbase的位置是。这段代码几乎是直接来自Murach的书中的第6章VB.Net数据库编程

和ado.net。感谢您的帮助。


Private Sub BindControls()

尝试

txtQuestion.DataBindings.Add(" Text", dsQuestion.Tables _

(" QS")," QText")

txtQuNum.DataBindings.Add(" Text",dsQuestion.Tables _

(QS),Q#)

cboQuNum.DataSource = dsQuestion.Tables(" QS")

cboQuNum。 DisplayMember =" Q#"

Catch ex As Exception

MessageBox.Show(ex.Message," Binding Controls")

结束尝试

End Sub


Private Sub btnAdd_Click(ByVal sender As System.Object,_

ByVal e As System.EventArgs )处理btnAdd.Click

尝试

bmbQuestion.AddNew()

SetButtons(False)

btnSave.Enabled = False

blnNewRow = True

txtQuestion.Clear()

txtQuNum.Clear()

txtQuNum.Focus ()

Catch ex As Exception

MessageB ox.Show(ex.Message,Error Adding Record)

结束尝试

结束子


Private Sub cboQuNum_SelectedIndexChanged (ByVal发送者为

System.Object,_

ByVal e As System.EventArgs)句柄

cboQuNum.SelectedIndexChanged

如果不是blnLoading那么

bmbQuestion.Position = cboQuNum.SelectedIndex

SetButtons(True)

bnnSave.Enabled = dsQuestion.HasChanges

txtQuNum.Focus()

结束如果

结束子

解决方案

< blockquote>嗨Greg,


我无法真正落后于你的代码,其中一件事是我认为

你有一个例行程序可以创建一个新的代码row。


Private Sub btnAdd_Click(ByVal sender As System.Object,_
ByVal e As System.EventArgs)处理btnAdd.Click
尝试
bmbQuestion.AddNew()




添加这个这里

\\\

BindingContext(DataSetName," TableName")。Position =

dataset.tables(0)。 rows.count -1

///

你能看到它对你有什么影响吗?


Cor


嗨Greg,


我无法真正落后于您的代码,其中一件事就是我认为

你有一个创建新行的例程。


Private Sub btnAdd_Click(ByVal sender As System.Object,_
ByVal e As System.EventArgs)处理btnAdd 。点击
尝试
bmbQuestion.AddNew()




在此添加

\\\

BindingContext(DataSetName," TableName")。Position =

dataset.tables(0).rows.count -1

///

你能看到它对你有什么影响吗?


Cor


谢谢,我''二试试看。我没有发布所有代码,因为大部分代码

似乎工作正常。就像我说的,除了几个例外,这是

与书中的代码(工作原理)相同。不同之处在于我正在使用

访问,而不是msde,并在组合框(而不是发布的一个

我不能在属性窗口中使用向导或设置数据绑定

,因为用户可以选择任何@ 60不同的数据库。每个客户端

都有自己的数据库,在一个单独的目录中等等(我没有设计这个

设置)。

也许是因为对此我也遇到问题,例如查询表达式中的语法错误

(缺少运算符)...例如,在删除命令后尝试更新

db时。命令制造者并没有
似乎正在接受绑定参数。

我会在周一回来工作时尝试你的建议。每次进行更改时我都可以执行

cmd.ExecuteNonQuery,但我想更新

只是数据集,然后立即提交所有更改。

Binding Manager & dataset - won''t add record
I''ve got an untyped dataset with controls bound through code. The user
can select a question number from a bound combobox, and the question
number and question text are displayed in bound textboxes. This part
works fine.
When I go to add a new record, the textboxes clear as they should. I
enter a new question number and tab to the textbox for the question
text. At this point, the text for the previously selected question
(whose number is still displayed in the combobox) reappears. If I then
change the text anyway and try to save, it tries to update the
previously selected record instead of adding a new one. I think it has
something to do with the position of the datset not being changed,
although the bindingmanagerbase position is. This code is almost
directly from chapter 6 in Murach''s book, VB.Net database programming
with ado.net. Thanks for any help.

Private Sub BindControls()
Try
txtQuestion.DataBindings.Add("Text", dsQuestion.Tables _
("QS"), "QText")
txtQuNum.DataBindings.Add("Text", dsQuestion.Tables _
("QS"), "Q#")
cboQuNum.DataSource = dsQuestion.Tables("QS")
cboQuNum.DisplayMember = "Q#"
Catch ex As Exception
MessageBox.Show(ex.Message, "Binding Controls")
End Try
End Sub

Private Sub btnAdd_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnAdd.Click
Try
bmbQuestion.AddNew()
SetButtons(False)
btnSave.Enabled = False
blnNewRow = True
txtQuestion.Clear()
txtQuNum.Clear()
txtQuNum.Focus()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error Adding Record")
End Try
End Sub

Private Sub cboQuNum_SelectedIndexChanged(ByVal sender As
System.Object, _
ByVal e As System.EventArgs) Handles
cboQuNum.SelectedIndexChanged
If Not blnLoading Then
bmbQuestion.Position = cboQuNum.SelectedIndex
SetButtons(True)
btnSave.Enabled = dsQuestion.HasChanges
txtQuNum.Focus()
End If
End Sub

解决方案

Hi Greg,

I cannot really get behind your code, one of the things is that i think that
you have a routine which create a new row.


Private Sub btnAdd_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnAdd.Click
Try
bmbQuestion.AddNew()



Adding this here
\\\
BindingContext(DataSetName, "TableName").Position =
dataset.tables(0).rows.count -1
///
And than you can see what that does for you?

Cor


Hi Greg,

I cannot really get behind your code, one of the things is that i think that
you have a routine which create a new row.


Private Sub btnAdd_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnAdd.Click
Try
bmbQuestion.AddNew()



Adding this here
\\\
BindingContext(DataSetName, "TableName").Position =
dataset.tables(0).rows.count -1
///
And than you can see what that does for you?

Cor


Thanks, I''ll try that. I didn''t post all the code because most of it
seemed to work ok. Like I said, with a couple exceptions, this is the
same as the code (that works) in a book. The differences are I''m using
Access, not msde, and calling the subs after a combobox (not the one
posted) selection instead of form load.
I can''t use wizards or set data binding in the Properties window
because the user can select any of @ 60 different db''s. Each client
has their own db, in a separate directory, etc (I didn''t design this
setup).
Maybe because of this I also get problems such as "Syntax error
(missing operator) in query expression..." when trying to update the
db after a delete command, for example. The commandbuilder doesn''t
seem to be picking up the bound parameters.
I''ll try your suggestion Monday when I''m back at work. I can do a
cmd.ExecuteNonQuery each time a change is made, but I want to update
just the dataset, then commit all changes at once.


这篇关于使用绑定在代码中的控件添加记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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