【问题标题】:CRUD Operations using DataGridView, DataTable and DataAdapter - Cannot add new row to DataGridView使用 DataGridView、DataTable 和 DataAdapter 的 CRUD 操作 - 无法向 DataGridView 添加新行
【发布时间】:2016-03-29 02:22:19
【问题描述】:

我正在尝试从 C# 界面网格视图将新记录插入到源表中...... 但是当我使用下面显示的 buttonclick 代码检索记录时...我在 gridview 中获取记录,但没有插入新记录的选项(附加屏幕截图).. 我可以从网格视图更新记录。

是否有任何选项或属性可以在 gridview 中启用插入选项?

按钮点击代码:

private void RetrieveRules_button_Click(object sender, EventArgs e)
{
    this.dataGridView.DataSource = null;
    this.dataGridView.Rows.Clear();


    SqlCommand cmd1 = con.CreateCommand();
    cmd1.CommandType = CommandType.Text;
    cmd1.CommandText = @" Select TOP 1 * FROM " + schemaName + "[ERSBusinessLogic] ORDER BY ERSBusinessLogic_ID     DESC";
    con.Open();
    cmd1.ExecuteNonQuery();
    DataTable dt = new DataTable();
    SqlDataAdapter DA = new SqlDataAdapter(cmd1);
    DA.Fill(dt);
    dataGridView.DataSource = dt;
    con.Close();

}

谢谢

【问题讨论】:

  • 我认为,datagridview 中有一个选项允许插入/更新/删除。单击datagridview,您将在控件的右上角看到一个类似三角形的选项,单击该选项,您将看到一个启用插入/更新和删除的选项。但是你必须做一些编码来实现它。快乐编码。
  • 您应该设置AllowUserToAddRows = true 让用户添加行。也用于删除AllowUserToDeleteRows = true 和编辑ReadOnly = false。此外,为了能够保存更改,您的数据适配器中应该有有效的插入、更新和删除命令。

标签: c# .net winforms datagridview


【解决方案1】:

使用 DataGridView、DataTable 和 DataAdapter 的 CRUD 操作

让用户使用DataGridView添加、删除或编辑行:

  1. AllowUserToAddRows 属性设置为true 或在DataGridView 任务 中,选中启用添加
  2. AllowUserToDeleteRows 属性设置为true 或在DataGridView 任务 中,检查启用删除
  3. ReadOnly 属性设置为false 或在DataGridView 任务 中,选中启用编辑

让用户使用 SqlDataAdapter 保存更改:

  1. 使用 select 语句和连接字符串创建 SqlDataAdapter
  2. 您的数据适配器应该有有效的InsertCommandDeleteCommandUpdateCommand。使用SqlCommandBuilder 创建有效命令。
  3. 使用数据适配器将数据加载到DataTable
  4. 将数据表设置为DataSource of DataGridView
  5. 在需要时使用SqlDataAdapter.Update 保存更改,方法是将数据表传递给方法。

代码

DataTable table;
SqlDataAdapter adapter;
private void Form1_Load(object sender, EventArgs e)
{
    //Create adapter
    var connection = @"your connection string";
    var command = "SELECT * FROM Table1";
    adapter = new SqlDataAdapter(command, connection);

    //Create Insert, Update and Delete commands
    var builder = new SqlCommandBuilder(adapter);

    //Load data
    table = new DataTable();
    adapter.Fill(table);

    //Bind the grid to data
    this.dataGridView1.DataSource = table;

    //Enable add, delete and edit
    this.dataGridView1.AllowUserToAddRows = true;
    this.dataGridView1.AllowUserToDeleteRows = true;
    this.dataGridView1.ReadOnly = false;
}

private void saveButton_Click(object sender, EventArgs e)
{
    //Save Data
    adapter.Update(table);
}

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
    adapter.Dispose();
}

注意

  • 你不需要那个ExecuteNonQuery。您只需要一个连接字符串和一个命令文本。然后您可以创建一个数据适配器。然后你甚至不需要管理打开和关闭连接,数据适配器会管理它。
  • 当您使用SELECT TOP 1 * 加载数据时,如果添加数据并保存,下次加载数据时将看不到更新,因为您只加载了一条记录。

【讨论】:

    【解决方案2】:

    代码使用ExecuteNonQuery,它返回的行数不受影响,而不是查询返回的结果。

    请改用ExecuteReader

    var reader = cmd1.ExecuteReader();
    DataTable dt = new DataTable();
    dtLoad(reader);
    

    是否有任何选项或属性可以启用插入选项 网格视图

    dataGridView1.ReadOnly = false;
    dataGridView1.EditMode = DataGridViewEditMode.EditOnF2; //if you want to edit on F2.
    

    【讨论】:

    • 问题与ExecuteNonQuery无关。 OP 正在使用SqlDataAdapter 加载数据,不需要执行也对结果没有任何影响。问题是他没有在网格中启用添加、编辑和删除功能。也为了保存数据,他没有有效的插入、更新、删除命令。见我的answer
    猜你喜欢
    • 1970-01-01
    • 2014-04-12
    • 1970-01-01
    • 2019-12-13
    • 2011-01-22
    • 2015-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多