[英]Check if a value already exist in db and bound dgv before adding/saving
I have a bound datagridview and I want to check before inserting a username if that username already exists or not in my database. 我有一个绑定的datagridview,我想在插入用户名之前进行检查,如果该用户名已存在或不在数据库中。 I insert values to dgv and database through a textBox.
我通过textBox将值插入dgv和数据库。 Thanks in advance..
提前致谢..
private void user1BindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.user1BindingSource2.EndEdit();
this.tableAdapterManager2.UpdateAll(this.databasDataSet);
}
private void Form2_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'databasDataSet.user1' table. You can move, or remove it, as needed.
this.user1TableAdapter2.Fill(this.databasDataSet.user1);
user1DataGridView.Columns[0].Visible = false;
passwordTextBox.PasswordChar ='*';
}
private void add_new_button_Click(object sender, EventArgs e)
{
this.user1BindingSource2.AddNew();
}
private void save_button_Click(object sender, EventArgs e)
{
this.Validate();
this.user1BindingSource2.EndEdit();
this.tableAdapterManager2.UpdateAll(this.databasDataSet);
}
private void delete_button_Click(object sender, EventArgs e)
{
this.user1BindingSource2.RemoveCurrent();
}
To validate anything on the server-side (the existence of the username in your case) there is CustomValidator
control. 为了验证服务器端的任何内容(在您的情况下,用户名是否存在),存在
CustomValidator
控件。 Add it to your page and attach it to your username TextBox
. 将其添加到页面并将其附加到用户名
TextBox
。 Then place your validation code to ServerValidate
event handler. 然后将您的验证代码
ServerValidate
事件处理程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.