我已经在网格视图中有一些数据。现在我想在gridview中插入多行那么怎么做呢?
如果我进行 foreach 循环,那么它将计算已经存在的所有行并多次插入数据。相反,我只想进入新行
下面是我的代码
private void userInsert()
{
if (MessageBox.Show("Do you want to add the new data ?", "Confirm ", MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.Yes)
{
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
try
{
foreach (GridViewRow dRow in userDataGridView.Rows)
{
cmd.CommandText = string.Format("insert into users(first_name,last_name,default_rate,default_location,spi_user_id,nickname) values('{0}','{1}',{2},{3},{4},'{5}')", userDataGridView.CurrentRow.Cells[0].Value.ToString(), userDataGridView.CurrentRow.Cells[1].Value.ToString(), userDataGridView.CurrentRow.Cells[2].Value.ToString(), locationID2ComboBox.SelectedValue, userDataGridView.CurrentRow.Cells[4].Value.ToString(), userDataGridView.CurrentRow.Cells[5].Value.ToString());
con.Open();
cmd.ExecuteNonQuery();
}
MessageBox.Show("Your data has been added successfully ", "Saved info", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
userSelect();
}
}
}