hello,
I got the below exception while trying to update the datagridview after adding a record into microsoft access database:
c# rows cannot be programmatically added to the datagridview's rows collection when control is data -bound
using below code:
private void btnAddOne_Click(object sender, EventArgs e)
{
string name = textBox1.Text.Trim();
string num = textBox2.Text.Trim();
OleDbConnection conn = null;
OleDbCommand cmd = null;
try
{
conn = new OleDbConnection(dbconnection);
conn.Open();
cmd = new OleDbCommand("INSERT into contact (name, mobile_num, date_created) " + " VALUES (@para1,@para2,@para3)", conn);
cmd.Parameters.AddWithValue("@para1", name);
cmd.Parameters.AddWithValue("@para2", num);
cmd.Parameters.AddWithValue("@para3", DateTime.Now.ToString("d/M/yyyy"));
cmd.ExecuteNonQuery();
this.dataGridView1.Rows.Add();
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
finally
{
cmd.Dispose();
conn.Close();
}
}
Appreciate if anyone can advice on this. thanks !
regards,
Mark