Hi,
There is any way to insert a record as it’s possible with edit if you want to edit a table from datagrid. For example I need to insert between row nº 3 and 4 a record. How can do this:
ID Date Account
1
2
3
//Insert here
4
5
When I need to edit a some rows I do this:
- conn = new SqlConnection("Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename=C:\\xxx\\xx\\App_Datas\\test.mdf;Integrated Security = True");
- comm = new SqlCommand();
- if (conn.State != ConnectionState.Open)
- conn.Open();
- SqlCommand comm1 = new SqlCommand();
- comm1.Connection = conn;
- SqlParameter dat = new SqlParameter("@dat", SqlDbType.Date);
- SqlParameter account = new SqlParameter("@account", SqlDbType.VarChar);
- SqlParameter ID = new SqlParameter("@ID", SqlDbType.Int);
- comm1.Parameters.Add(dat);
- comm1.Parameters.Add(account);
- comm1.Parameters.Add(ID);
- data.Value = dtDate.Text;
- conta.Value = txtAccount.Text;
- ID.Value = lblID.Text;
- comm1.Connection = conn;
- comm1.CommandText = "UPDATE test1 SET date = @date, account = @ account, WHERE (ID = @ID)";
- {
- if (MessageBox.Show("Do you want to insert this record?", "Insert Record", MessageBoxButtons.YesNo) == DialogResult.Yes)
- {
- try
- {
- comm1.ExecuteNonQuery();
- }
- finally
- {
- dgvTest.CurrentCell = dgvTest.Rows[dgvTest.Rows.Count - 1].Cells[0];
- conn.Close(); }
- }