在添加/保存之前,检查db和绑定dgv中是否已存在值

时间:2015-03-15 21:50:42

标签: c# datagridview

我有一个绑定的datagridview,我想在插入用户名之前检查,如果该用户名已存在或不存在于我的数据库中。我通过textBox向dgv和数据库插入值。提前谢谢..

    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();

    }

1 个答案:

答案 0 :(得分:0)

要验证服务器端的任何内容(在您的情况下是否存在用户名),有CustomValidator控件。将其添加到您的网页并将其附加到您的用户名TextBox。然后将验证代码放到ServerValidate事件处理程序。