如何在数据集中插入新的datagridview行并在数据库中更新? [英] How to insert new datagridview row in dataset and update in database?

查看:111
本文介绍了如何在数据集中插入新的datagridview行并在数据库中更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有



我开始用C#编程。我有问题:我创建了项目表,其中包含2行,编程为

Hi, all

I started programming in C#. I have problem: I created "Projects" table with 2 rows which are programmed in

get; set; 

class:ID,PROJECTS_NAME。我已经完成了在此表中编辑值:



class: ID, PROJECTS_NAME. I have already done with editing values in this table:

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);
           }
       }





项目中的SQL列表:



SQL Columns in projects Table:

PROJECT_NAME VARCHAR
ID INT



它运行正常。但我在插入新行并自动保存在数据库中时遇到问题。



我尝试过:



我尝试过RowValidated事件:




And it works fine. But i have a problem with inserting new row and saving automatically in database.

What I have tried:

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);
         }





但由于



But it didn't work because of

System.FormatException: "Invalid input string format."





有人可以解释一下如何解决它吗?我搜索了一些东西,但我不知道如何处理。问候语



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

推荐答案

你必须使用
UserAddedRow

投入

RowValidated







 private void dataGridView4_UserAddedRow(object sender, DataGridViewRowEventArgs e)
     {
         string name_project = e.Row.Cells.Cells[1].Value.ToString();
         int id = 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);
      }   
}


这篇关于如何在数据集中插入新的datagridview行并在数据库中更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆