点击新增按键datagridview新增一行

怎样点击新增按键在datagridview中新增一行,并且可以在其中填入新的数据,保存后同步到数据库。

在 WinForm 应用程序中,可以使用 DataGridView 控件来显示和编辑数据。可以通过以下步骤来实现在 DataGridView 中点击新增按钮添加新行并保存到数据库中的功能:

添加 DataGridView 控件和新增按钮到窗体上。可以通过 Visual Studio 的设计器来实现。

为新增按钮添加 Click 事件的处理函数。在事件处理函数中,可以通过 DataGridView 的 Rows 属性添加新行。

private void btnAdd_Click(object sender, EventArgs e)
{
    dataGridView.Rows.Add();
}


在 DataGridView 中,可以使用 CellEndEdit 事件来捕获单元格编辑结束的事件,这时可以将修改后的数据同步到数据库中。需要注意的是,DataGridView 中的新增行默认是空行,需要手动判断并过滤掉。

private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    // 只处理非新增行
    if (e.RowIndex >= dataGridView.Rows.Count - 1)
    {
        return;
    }

    // 获取修改后的数据
    int id = Convert.ToInt32(dataGridView.Rows[e.RowIndex].Cells["ID"].Value);
    string name = dataGridView.Rows[e.RowIndex].Cells["Name"].Value.ToString();
    // ...

    // 更新数据库
    SqlConnection conn = new SqlConnection("server=localhost;database=mydb;uid=myuser;pwd=mypassword;");
    conn.Open();
    SqlCommand cmd = new SqlCommand("UPDATE mytable SET Name=@Name WHERE ID=@ID", conn);
    cmd.Parameters.AddWithValue("@ID", id);
    cmd.Parameters.AddWithValue("@Name", name);
    // ...
    cmd.ExecuteNonQuery();
    conn.Close();
}


完整的示例代码如下:

using System;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace DataGridViewExample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // 初始化 DataGridView 数据
            SqlConnection conn = new SqlConnection("server=localhost;database=mydb;uid=myuser;pwd=mypassword;");
            conn.Open();
            SqlCommand cmd = new SqlCommand("SELECT * FROM mytable", conn);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                dataGridView.Rows.Add(reader["ID"], reader["Name"], reader["Age"]);
            }
            conn.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            dataGridView.Rows.Add();
        }

        private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            // 只处理非新增行
            if (e.RowIndex >= dataGridView.Rows.Count - 1)
            {
                return;
            }

            // 获取修改后的数据
            int id = Convert.ToInt32(dataGridView.Rows[e.RowIndex].Cells["ID"].Value);
            string name = dataGridView.Rows[e.RowIndex].Cells["Name"].Value.ToString();
            int age = Convert.ToInt32(dataGridView.Rows[e.RowIndex].Cells["Age"].Value);

            // 更新数据库
            SqlConnection conn = new SqlConnection("server=localhost;database=mydb;uid=myuser;pwd=mypassword;");
            conn.Open();
            SqlCommand cmd = new SqlCommand("UPDATE mytable SET Name=@Name, Age=@Age WHERE ID=@ID", conn);
            cmd.Parameters.AddWithValue("@ID", id);
            cmd.Parameters.AddWithValue("@Name", name);
            cmd.Parameters.AddWithValue("@Age", age);
            cmd.ExecuteNonQuery();
            conn.Close();
        }
    }
}


答案出自c# https://www.wodianping.com/