使用 DataGridView、DataTable 和 DataAdapter 的 CRUD 操作 - 无法向 DataGridView 添加新行 [英] CRUD Operations using DataGridView, DataTable and DataAdapter - Cannot add new row to DataGridView
问题描述
我正在尝试从 C# 界面网格视图向源表中插入新记录....但是,当我使用下面显示的按钮单击代码检索记录时......我在 gridview 中获取记录但没有插入新记录的选项(附上屏幕截图)......因为我可以从网格视图更新记录.
I'm trying to insert new records into the source table from the C# interface grid view.... But when I retrieve the records with the buttonclick code shown below... Im getting records in the gridview but no option for inserting new records(screen shot attached).. where as i can update the reocrds from the grid view.
是否有任何选项或属性可以在 gridview 中启用插入选项?
Is there any option or property for enabling the insertion option in the 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();
}
谢谢
推荐答案
CRUD Operations using DataGridView, DataTable and DataAdapter
要让用户使用 DataGridView
添加、删除或编辑行:
CRUD Operations using DataGridView, DataTable and DataAdapter
To let the user add, delete or edit rows with DataGridView
:
- 设置
AllowUserToAddRows
属性为 true 或在 DataGridView Tasks 中,选中 Enable Adding - 设置
AllowUserToDeleteRows
属性为 true 或在 DataGridView Tasks 中,选中 Enable Deleting - 设置
ReadOnly
属性设置为 false 或在 DataGridView Tasks 中,选中 Enable Editing
- Set
AllowUserToAddRows
property to true or in DataGridView Tasks, check Enable Adding - Set
AllowUserToDeleteRows
property to true or in DataGridView Tasks, check Enable Deleting - Set
ReadOnly
property to false or in DataGridView Tasks, check Enable Editing
让用户使用 SqlDataAdapter 保存更改:
To let the user save changes using a SqlDataAdapter:
- 创建一个
SqlDataAdapter
使用选择语句和连接字符串. - 您应该具有有效的
InsertCommand
,DeleteCommand
和UpdateCommand
用于您的数据适配器.使用 创建有效命令SqlCommandBuilder
. - 将数据加载到
DataTable
使用数据适配器. - 设置数据表为
DataGridView
的 - 当您需要使用
SqlDataAdapter.Update
通过将数据表传递给方法.
DataSource
代码
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 *
加载数据时,如果添加数据并保存,下次加载数据时将看不到更新,因为您只加载了一条记录.
- You don't need that
ExecuteNonQuery
. You only need a connection string and a command text. Then you can create a data adapter. Then you even don't need to manage opening and closing the connection, data adapter manages it. - When you load data using
SELECT TOP 1 *
, if you add data and save, you can't see updates next time you load the data because you load only a single record.
这篇关于使用 DataGridView、DataTable 和 DataAdapter 的 CRUD 操作 - 无法向 DataGridView 添加新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!