问题描述
我环顾四周,找到了很多解决方案,但他们要么没有足够的理由适用于我的具体问题,要么他们不工作。
继承人我的意思拥有:
1包含datagridview的表单,即supposto包含一堆用户名和密码。
用户从未看到此表单。
另一种形式 - 用户看到,要求他们输入用户名和密码,然后是登录按钮。
按下按钮后,程序运行在datagridview上的每个项目,直到找到你输入的确切用户名,如果找到它,它会看到附加到该用户名的密码是否匹配,如果找不到用户名,或者密码不匹配,它返回用户说错误你输入的用户名或密码未找到(取决于哪个字段错误)
一切正常。
但是......无论出于什么原因我都无法访问这个数据库,除非我访问它笨拙......
所以,继续我的问题:
我在datagridview中添加了一个新行,然后才查看它(因为无论出于何种原因,它永远不会保存项目,所以它在数据库中的alaways -1行),然后我为第一个字段(用户名字段)操纵它为例如Bob - 然后我检查了,如果用户名他们输入(再次,鲍勃,例如),与数据库中的名称相同,然后说你登录,否则说失败。
什么它确实是,它崩溃说出以下错误:
当控件受数据绑定时,无法以编程方式将行添加到DataGridView的行集合中。
所以,我的问题:
我如何1.
解除绑定这个数据绑定控件?
或2.
编辑没有很多复杂代码的行?
这是我现在的代码:
Iv looked around, and found many solutions, but they either dont make enough sense to apply to my specific issue, or they dont work.
Heres what I have:
1 form that holds a datagridview, that is supposto contain a bunch of usernames and passwords.
This form is never seen by the user.
Another form - in which the user sees, that asks them for a username and password, then a "Log In" button.
Upon pressing the button, the program runs through every item on the datagridview until it finds the exact username you typed in, if it finds it, it sees if the password attached to that username matches, if it cannot find the username, or the password doesnt match, it returns a error to the user saying "the username or password you entered was not found" (depending on which field as wrong)
That all works fine.
But... For whatever reason I cant access this database, unless I access it programmically...
So, heres my problem:
I added a new row to the datagridview, before it looks through it (because for whatever reason it would never save the items, so its alaways -1 rows in the database), then I rigged it for that first field (Username field) to be "Bob" for example - then I had it check, if the username they entered (Again, Bob for example), is the same as the name in the database, then say your logged in, otherwise, say "failed".
What it does is, its crashing saying the following error:
Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.
So, my question:
How do I either 1.
Unbind this data-bound control?
or 2.
Edit the rows without alot of complicated code?
Here is the code I have now:
Form2.DataGridView1.Rows.Add(1)
Form2.DataGridView1.Rows(0).Cells(0).Value = "Bob"
If Textbox1.Text = Form2.DataGridView1.Rows(0).Cells(0).Value Then
MessageBox.Show("Logged In.")
Else
MessageBox.Show("Failed.")
End If
任何人都可以给我一些示例代码?非常感谢。
此外,datagridview永远不会在运行时编辑,我不能在设计视图中手动添加项目。
Could anyone give me some sample code aswell? That would be greatly appreciated.
Also, the datagridview is never edited during runtime, and I cant manually add items to it in the design view.
推荐答案
这篇关于解绑数据绑定控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!