编辑数据库时,然后在datagridview中添加多行,则这些行重复

时间:2018-11-19 05:37:37

标签: c# ms-access datagridview

我有一个程序,用户可以在其中添加配置文件,也可以对其进行编辑。我想使datagrid视图更易于查看,因此在其中添加了多行,但是当我回去编辑该行,对数据库进行更改然后保存时,它会添加一组全新的行!我该如何预防?我尝试使用dataGridView1.Rows.Clear();清除datagridview,但这引发了此错误:System.ArgumentException:'无法清除此列表。 这是我编写的添加多行代码:

        private void pictureBox1_Click(object sender, EventArgs e)
    {
        tabTasks.SelectedIndex = 1;
        RefreshDBConnection();
        dataGridView1.Rows[0].Cells["FIRST NAME"].Value = dataGridView1.Rows[0].Cells["FIRST NAME"].Value + " " + dataGridView1.Rows[0].Cells["LAST NAME"].Value + Environment.NewLine + dataGridView1.Rows[0].Cells["ADDRESS #1"].Value + " " + dataGridView1.Rows[0].Cells["ADDRESS #2"].Value + Environment.NewLine + dataGridView1.Rows[0].Cells["CITY"].Value + ", " + dataGridView1.Rows[0].Cells["STATE"].Value + " " + dataGridView1.Rows[0].Cells["ZIP CODE"].Value + Environment.NewLine + dataGridView1.Rows[0].Cells["COUNTRY"].Value + Environment.NewLine + dataGridView1.Rows[0].Cells["PHONE"].Value + Environment.NewLine + dataGridView1.Rows[0].Cells["EMAIL"].Value;
    }

如果有帮助,以下是用于编辑配置文件的行:

        private void buttonSave_Click(object sender, EventArgs e)
    {
        ConnectToDataBase();
        OleDbCommand command = new OleDbCommand();
        command.Connection = connection;
        //string query = "update Profiles set [PROFILE NAME]='" + textBox1.Text + "', [LOGIN EMAIL]='" + textBox2.Text + "', [PASSWORD]='" + textBox3.Text + "', [FULL NAME]='" + textBox4.Text + "', [CARD NUMBER]='" + textBox5.Text + "', [EXP MONTH]='" + comboBox1.Text + "', [EXP YEAR]='" + comboBox2.Text + "', CVV='" + textBox6.Text + "' where  ID='" + Convert.ToInt32(textBox7.Text) + "'";
        string query = "update Profiles set [PROFILE NAME]= @Profile, [FIRST NAME]= @FName, [LAST NAME]= @LName, [ADDRESS #1]= @Add1, [ADDRESS #2]= @Add2, [CITY]= @City, [ZIP CODE]= @Zip, [STATE]= @State, [COUNTRY]= @Country, [PHONE]= @Phone, [EMAIL]= @Email, [CARD TYPE]= @CardType, [CARD NUMBER]= @CardN, [EXP MONTH]= @EXPM, [EXP YEAR]= @EXPY, CVV= @CVV where ID = @Id";
        command.Parameters.AddWithValue("@Profile", txtProfile.Text);
        command.Parameters.AddWithValue("@FName", txtFirstName.Text);
        command.Parameters.AddWithValue("@LName", txtLastName.Text);
        command.Parameters.AddWithValue("@Add1", txtAddress1.Text);
        command.Parameters.AddWithValue("@Add2", txtAddress2.Text);
        command.Parameters.AddWithValue("@City", txtCity.Text);
        command.Parameters.AddWithValue("@Zip", txtZip.Text);
        command.Parameters.AddWithValue("@State", comState.Text);
        command.Parameters.AddWithValue("@Country", comCountry.Text);
        command.Parameters.AddWithValue("@Phone", txtPhone.Text);
        command.Parameters.AddWithValue("@Email", txtEmail.Text);
        command.Parameters.AddWithValue("@CardType", comCardType.Text);
        command.Parameters.AddWithValue("@CardN", txtCard.Text);
        command.Parameters.AddWithValue("@EXPM", comboEXPM.Text);
        command.Parameters.AddWithValue("@EXPY", comboEXPY.Text);
        command.Parameters.AddWithValue("@CVV", txtCVV.Text);
        command.Parameters.AddWithValue("@ID", txtID.Text);
        command.CommandText = query;
        command.ExecuteNonQuery();
        RefreshDBConnection();
        connection.Close();
        MessageBox.Show("Profile Edited");
        this.Close();
    }

最后,这是当我编辑然后保存时发生的情况的图片,由于某些明显的原因,一些信息被屏蔽了:Error with lines

如果可以给您更多信息,请告诉我,谢谢! (使用c#btw)

0 个答案:

没有答案