使用DataGridView,DataTable和DataAdapter的CRUD操作-无法向DataGridView添加新行 [英] CRUD Operations using DataGridView, DataTable and DataAdapter - Cannot add new row to DataGridView
问题描述
我正在尝试从C#接口网格视图向源表中插入新记录...。
但是,当我使用如下所示的buttonclick代码检索记录时...我正在gridview中获取记录但没有插入新记录的选项(附有屏幕截图)。在这里,我可以从网格视图更新reocrds。
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.
是否有任何选项或属性可以启用
Is there any option or property for enabling the insertion option in the gridview ?
Buttonclickcode:
Buttonclickcode :
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,DataTable和DataAdapter的CRUD操作
要让用户使用 DataGridView
添加,删除或编辑行:
CRUD Operations using DataGridView, DataTable and DataAdapter
To let the user add, delete or edit rows with DataGridView
:
- 设置
AllowUserToAddRows
属性设置为true或在 DataGridView任务中,选中启用添加 - 设置
AllowUserToDeleteRows
属性设置为true或在 DataGridView Tasks ,选中启用删除 - 设置
ReadOnly
属性设置为false或在 DataGridView中任务,选中启用编辑
- 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
使用select语句和连接字符串。 - 您应该具有有效的
InsertCommand
,DeleteCom mand
和UpdateCommand
用于您的数据适配器。使用创建有效的命令SqlCommandBuilder
。 - 将数据加载到
DataTable
使用数据适配器。 - 将数据表设置为
DataGridView
的 - 在需要使用
SqlDataAdapter.Update
通过将数据表传递给方法。
DataSource
b - Create a
SqlDataAdapter
using a select statement and a connection string. - You should have valid
InsertCommand
,DeleteCommand
andUpdateCommand
for your data adapter. Create valid commands using aSqlCommandBuilder
. - Load data to a
DataTable
using data adapter. - Set data table as
DataSource
ofDataGridView
- Save changes when you need using
SqlDataAdapter.Update
by passing data table to the method.
代码
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 * $ c加载数据时$ c>,如果您添加数据并保存,则下次加载数据时将看不到更新,因为您仅加载一条记录。
- 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屋!