In this case I display values into datagridview from Projects table which it has only 2 columns:

PROJECT_NAME VARCHAR
ID INT // Which is hidden in datagridview, and as AUTOINCREMENT

I have already done with editing values in this table and it works fine:

`private void dataGridView4_CellEndEdit(object sender, DataGridViewCellEventArgs e)

               {
                   string name_project = dataGridView4.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();    

           string id = dataGridView4.Rows[e.RowIndex].Cells[0].Value.ToString();

       MySqlConnection connection = new MySqlConnection("datasource = localhost; port = 3306; username = root; password = ");
       MySqlCommand cmd = new MySqlCommand("UPDATE projekt1.projects SET PROJECTS_NAME = @PROJECTS_NAME WHERE ID = @ID", connection);

       cmd.Parameters.Add("@ID", MySqlDbType.Int64).Value = int.Parse(id);
       cmd.Parameters.Add("@PROJECTS_NAME", MySqlDbType.VarChar).Value = name_project;

       try
       {
           connection.Open();
           cmd.ExecuteNonQuery();
           connection.Close();
       }
       catch (Exception ex)
       {
           MessageBox.Show(ex.Message);
       }
   }`

But i have a problem with inserting new row and saving automatically in database.

What i've tried?

1) I've tried with RowValidated Event:

private void dataGridView4_RowValidated(object sender, DataGridViewCellEventArgs e)
     {
         string name_project = dataGridView4.Rows[e.RowIndex].Cells[1].Value.ToString();

     string id = dataGridView4.Rows[e.RowIndex].Cells[0].Value.ToString();

     MySqlConnection connection = new MySqlConnection("datasource = localhost; port = 3306; username = root; password = ");
     MySqlCommand cmd = new MySqlCommand("INSERT INTO projekt1.projects(PROJECT_NAME) VALUES(@NPROJECT_NAME);", connection);

     cmd.Parameters.Add("@ID", MySqlDbType.Int64).Value = int.Parse(id);
     cmd.Parameters.Add("@PROJECT_NAME", MySqlDbType.VarChar).Value = name_project;

     try
     {
         connection.Open();
         cmd.ExecuteNonQuery();
         connection.Close();

         MessageBox.Show("ok");
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }

     try
     {
         connection.Open();
         cmd.ExecuteNonQuery();
         connection.Close();
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }

     `Inline Code Example Here`

But it didn't work because of

System.FormatException: "Invalid input string format."

2) I've tried that UserAddedRow event too(which will be in code snippet).
but then have that exception in below:

System.NullReferenceException: The object reference has not been set to the instance of the object. System.Windows.Forms.DataGridViewCell.Value.get returned null.”

Then i tried removing 2 lines of code:

 int id = (int)Convert.ToInt64(e.Row.Cells[0].Value.ToString());   

cmd.Parameters.Add("@ID", MySqlDbType.Int64).Value = id;

And it didn't help.

Can someone please write how to solve it? I've searched something but i don't know how to deal with. Greetings.

private void dataGridView4_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {

            string name_project= e.Row.Cells[1].Value.ToString();
            int id = (int)Convert.ToInt64(e.Row.Cells[0].Value.ToString());

            MySqlConnection connection = new MySqlConnection("datasource = localhost; port = 3306; username = root; password = ");
            MySqlCommand cmd = new MySqlCommand("INSERT INTO projekt1.projects(PROJECT_NAME) VALUES(@NPROJECT_NAME);", connection);

            cmd.Parameters.Add("@ID", MySqlDbType.Int64).Value = id;

            cmd.Parameters.Add("@PROJECT_NAME", MySqlDbType.VarChar).Value = name_project;

            try
            {
                connection.Open();
                cmd.ExecuteNonQuery();
                connection.Close();

                MessageBox.Show("ok");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.