如何在datagridview中插入新的datagridview行并在数据库中(自动)更新?

时间:2019-07-14 11:08:05

标签: c# mysql .net datagridview insert

在这种情况下,我从只有两列的Projects表中将值显示到datagridview中:

Model

我已经完成了在此表中编辑值的工作,并且工作正常:

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

但是我在插入新行并自动保存到数据库时遇到问题。

我尝试了什么?

1)我尝试过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); } } 事件:

RowValidated

但由于以下原因,它不起作用

  

System.FormatException:“无效的输入字符串格式。”

2)我也尝试过该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); } 事件:

UserAddedRow

,但下面有该异常:

  

System.NullReferenceException:未设置对象引用   到对象的实例。   System.Windows.Forms.DataGridViewCell.Value.get返回null。”

然后我尝试删除2行代码:

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

它没有帮助。

有人可以写出解决方法吗?我已经搜索过一些东西,但我不知道该如何处理。问候。

1 个答案:

答案 0 :(得分:0)

您可以使用ShowNewButtonInHeader属性

<Columns>
<dx:GridViewCommandColumn ShowNewButtonInHeader="true </dx:GridViewDataTextColumn>
</Columns>