- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试从 C# 界面 GridView 将新记录插入到源表中....但是当我使用下面显示的 buttonclick 代码检索记录时...我在 gridview 中获取记录但没有插入新记录的选项(附有屏幕截图)..我可以从 GridView 更新 reocrds。
是否有任何选项或属性可以在 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
添加、删除或编辑行:
AllowUserToAddRows
属性设置为 true 或在 DataGridView Tasks 中,勾选Enable AddingAllowUserToDeleteRows
属性设置为 true 或在 DataGridView Tasks 中,勾选Enable DeletingReadOnly
属性设置为 false 或在 DataGridView Tasks 中,勾选Enable Editing让用户使用 SqlDataAdapter 保存更改:
SqlDataAdapter
使用选择语句和连接字符串。InsertCommand
, DeleteCommand
和 UpdateCommand
为您的数据适配器。使用 SqlCommandBuilder
创建有效命令.DataTable
使用数据适配器。DataGridView
的DataSource
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 *
加载数据时,如果您添加数据并保存,则下次加载数据时您将看不到更新,因为您只加载了一条记录。关于c# - 使用 DataGridView、DataTable 和 DataAdapter 的 CRUD 操作 - 无法向 DataGridView 添加新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36274296/
我有这样的代码: var ds = new DataSet(); var fooIDToFoo = new Dictionary {{"Foo1", foo1}, {"Food2", foo2}};
我编写了一组异步方法来快速处理大量数据库拉取和编译。在大多数情况下,这些工作非常棒,并且确实为我的软件创造了奇迹。然而,最近我在这些方法上遇到了一个小问题:用户时不时地搞砸了,软件拉取数据的时间范围变
我从HERE复制了一些代码,但出现错误:“')'附近的语法不正确。 但是我看不到这段代码有什么问题 Dim conString As String = "Data Source=.\SQLEXPRES
我们使用 VB.net 对数据读取器(7 条记录)执行 mySQL 语句,然后通过下面的代码加载到数据表(新创建的 6 条记录?) - 当涉及到数据表时,第一条记录似乎潜逃了 - 我无法理解为什么 -
众所周知,DataAdapter 会打开和关闭尚未打开的连接。但是用我的代码它打开但没有关闭..我正在使用 MySql.Data.MySqlClient.MySqlDataAdapter,不确定我做错
我目前正在学习 Web 开发人员,并且正在学习 ASP.NET WEBFORMS。我对 DataAdapter 和更新/删除表有疑问。 我想知道哪种方法是正确的。假设我有这种方法。 更新方法 publ
我尝试在 C#.net 中使用 DataAdapter。但我仍然不了解 DataAdapter。 我阅读了很多关于 DataAdapter 和 DataReader 的文章和博客。 我知道 DataA
我想使用 DataAdapter 填充 DataGridView 控件。但是我不知道该怎么做,因为我使用的是带参数的存储过程。有人可以举个例子吗? 最佳答案 我明白了!...嘿嘿 protected
数据未显示。我尝试在调试器中调试cide。我收到错误 $.jqx.dataAdapter is not a constructor 数据已成功填充到 aDataSet 中。请告诉我什么可能是错误的。
我已经初始化了一个数据适配器: string sql = "SELECT * From localitati"; da1 = new System.Data.SqlClient.SqlDataAdap
我有一个断开连接的 dataTable,其中包含一些记录。 我正在使用以下函数获取 dataTable。 static System.Data.DataTable ReadSetUpTable(str
我有一个相对简单的例程,它查看媒体文件的数据库条目,计算宽度、高度和文件大小,然后将它们写回数据库。 数据库是 SQLite,使用 System.Data.SQLite 库,处理大约 4000 行。我
我正在尝试按照标题所说的进行操作,但在运行时出现此错误: Incorrect syntax near the keyword 'Top'. string connString = @"server
我知道可能有一个明显的原因,但我找不到.. 我通常在连接数据库和读取数据时使用using语句,但我不能在DataAdapter上使用它,因为它没有实现IDisposable. 另外:填充数据集后它的行
我知道 DataAdapter 存在性能问题,但是否有任何可能更快的解决方法?目前,DataAdapter.Fill 方法在处理 3000 条记录时需要 5-6 秒,这对我的应用来说太慢了。如果我删除
我试图从 MySQL 和 Oracle 数据库加载大的十进制值,这会导致异常。据我所知,当它尝试将值转换为 .Net 的十进制数据类型时,会导致异常,但是从数据库读取如此巨大的值的方法是什么? MyS
我在 vb.net 中使用 MySQLDatagridview正确显示,一切正常,当我尝试用更新(方法)更新数据库中的更改时出现问题默认情况下,数据在内存中更新“被删除的学生 ID = 2”但是数据库
我正在尝试使用 cwac 提供的演示......但是当我将它拉入我的 fragment 时我收到错误“方法 getActivity() 未定义类型 DataAdapter” 我确信它是上下文相关的,所
我正在处理一些遗留代码,试图了解它是如何工作的。 该代码有一个 MySqlCommand,它调用一个存储过程来填充一个 DataTable,如下所示: _apptCmd = new MySql
以下代码未通过 dataadapter.update() 将数据集的更改保存到数据库。我将 winform 上的数据显示到文本框。 我有一个保存按钮,用于保存对数据库所做的更改。更改仅保存到数据集的内
我是一名优秀的程序员,十分优秀!