Datagridview选择我通过数据库插入的最后一个插入行

时间:2017-05-19 15:25:52

标签: c# mysql datagridview

我想选择数据库中最后一次插入的行。当我在数据库中插入数据时,datagridview被刷新,并且他在最后一行中检索我插入的内容。我的问题是我不知道如何选择最后一个插入行。我有最后一个插入ID,现在怎么样 enter image description here

当我点击保存按钮时,新项目被添加到数据库,网格刷新并显示新行。如何自动成功后保存数据选择行如 id 30 in grid

private void sacuvajPromeneButton_Click(object sender, EventArgs e)
{
    using (var pleaseWaitForm = new Loading())
    {
        pleaseWaitForm.Show();

        Application.DoEvents();

        using (var conn = new MySqlConnection(Database.connStringStatic))
        {
            try
            {
                conn.Open();

                using (MySqlCommand cmd = conn.CreateCommand())
                {

                    cmd.Parameters.AddWithValue("@ime", primalac_ime_textBox.Text);
                    cmd.Parameters.AddWithValue("@prezime", primalac_prezime_textBox.Text);
                    cmd.Parameters.AddWithValue("@grupa_id", 1);
                    cmd.Parameters.AddWithValue("@ulica", primalac_ulica_textBox.Text);
                    cmd.Parameters.AddWithValue("@grad", primalac_grad_textBox.Text);
                    cmd.Parameters.AddWithValue("@postanski_br", primalac_postanskiBroj_textBox.Text);
                    cmd.Parameters.AddWithValue("@jmbg", posiljalac_jmb_textBox2.Text);
                    cmd.Parameters.AddWithValue("@telefon", primalac_kontakt_textBox.Text);
                    cmd.Parameters.AddWithValue("@telefon_2", telefon2_textBox1.Text);
                    cmd.Parameters.AddWithValue("@email", posiljalac_email_textBox3.Text);
                    cmd.Parameters.AddWithValue("@napomena", napomenarichTextBox1.Text);
                    cmd.Parameters.AddWithValue("@datum_kreiranja", DateTime.Now);

                   // cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "CALL kreiraj_korisnika(@ime, @prezime, @grupa_id, @ulica, @grad, @postanski_br, @jmbg, @telefon, @telefon_2, @email, @napomena, @datum_kreiranja)";
                    cmd.ExecuteNonQuery();

                    long lastInsertId= cmd.LastInsertedId;

                    // Refresh grid
                    dataGridView1.DataSource = null;
                    bs.DataSource = GetDat("SELECT id, ime, prezime, ulica, grad, postanski_br, telefon FROM korisnici ORDER by id");
                    dataGridView1.DataSource = bs;


                   // Test selecting


                    pleaseWaitForm.Close();

                    MessageBox.Show("User created.");

                    // Now select that new created user in grid
                   // dataGridView1.Rows[4].Selected = true;


                    this.Activate();

                    sacuvajPromeneButton.Visible = false;


                }
            }
            catch(MySqlException ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

    }
}

2 个答案:

答案 0 :(得分:2)

您可以通过处理DataGridViewRowsAddedEvent

来选择最新添加的行
private void DataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
    this.Rows[e.RowIndex].Selected = true;
} 

答案 1 :(得分:0)

上面的代码不起作用。

进行以下更改。

然后,将多选选项设置为false。

private void DataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
    dataGridView1.Rows[e.RowIndex].Selected = true;
}