javascript - asp.net 使用 sql 数据创建动态网格并使其可编辑
问题描述
我正在尝试实施一个新项目。我想将一个 SQL 表动态地写入例如 asp:gridview。这个网格应该是可编辑的。
我找到了一些解决方案,但它们都是静态的。但是表格必须是动态的,因为我想在网格中包含不同的 SQL 表格。(SQL 表有时会更改、删除列或添加新列。因此,静态解决方案不适用。
理想情况下,编辑功能应该像在 Excel 文档中一样工作。更改数据后,应使用保存按钮将其写回 SQL 表。
这甚至可能吗?至少在过去的 4 小时内,我还没有找到解决此问题的方法。
谢谢您的帮助
问候
更新
//HTML Code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Personaldaten</h1>
<asp:GridView ID="personal_data" AutoGenerateColumns="true" runat="server"></asp:GridView>
<asp:Button runat="server" ID="button1" text="Save"/>
</div>
</form>
</body>
</html>
//Code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace Editable_SQL_Table
{
public partial class table : System.Web.UI.Page
{
SqlDataAdapter sda;
SqlCommandBuilder scb;
DataTable dt;
public form1()
{
InitializeComponent();
}
public void form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Server = (localdb)\\MSSQLLocalDB; Database = Personal; Trusted_Connection = True; MultipleActiveResultSets = true");
sda = new SqlDataAdapter(@"SELECT* FROM dbo.MyTable", con);
dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
}
private void button1_Click(object sender, EventArgs e)
{
scb = new SqlCommandBuilder(sda);
sda.Update(dt);
MessageBox.Show("Table updated");
}
}
}
我尝试使用来自 user2980341 的编码,但“dataGridView1.DataSource = dt;”有问题 我认为对于“dataGridView1”,我应该使用我的 Gridview 中的 id,但我不工作。调试器说,他不认识“personal_data”。还有函数“InitializeComponent();” 未定义。
感谢您提供任何进一步的帮助,我目前正在研究以下解决方案:
//HTML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="table.aspx.cs" Inherits="Editable_SQL_Table.table" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Personaldaten</h1>
<asp:GridView ID="personal_data" AutoGenerateEditButton="true" AutoGenerateColumns="true" runat="server" OnRowUpdating="personal_data_RowUpdating" OnRowCancelingEdit="personal_data_RowCancelingEdit" OnRowEditing="personal_data_RowEditing"></asp:GridView>
</div>
</form>
</body>
</html>
//Code behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace Editable_SQL_Table
{
public partial class table : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string table = "dbo.Mitarbeiter";
string query = "SELECT * FROM" + " " + table;
string ConnectionString = "Server = (localdb)\\MSSQLLocalDB; Database = Personal; Trusted_Connection = True; MultipleActiveResultSets = true";
using (SqlConnection myConnection = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(query, myConnection))
{
myConnection.Open();
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
DataTable dt = new DataTable();
dt.Load(dr);
personal_data.DataSource = dt;
personal_data.DataBind();
}
}
}
protected void personal_data_RowEditing(object sender, GridViewEditEventArgs e)
{
personal_data.EditIndex = e.NewEditIndex;
}
protected void personal_data_RowUpdating(object sender, GridViewEditEventArgs e)
{
//personal_data.EditIndex = e.NewEditIndex;
}
protected void personal_data_RowCancelingEdit(object sender, GridViewEditEventArgs e)
{
//personal_data.EditIndex = e.NewEditIndex;
}
}
}
解决方案
可能是这样的:在表单上添加 DataGridView 和一个 Button 元素。然后将此代码附加到您的元素:
using System.Data.SqlClient;
namespace test
{
public partial class Form1 : Form
{
SqlDataAdapter sda;
SqlCommandBuilder scb;
DataTable dt;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection (@"Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;");
sda = new SqlDataAdapter(@"SELECT* FROM dbo.MyTable", con);
dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
}
private void button1_Click(object sender, EventArgs e)
{
scb = new SqlCommandBuilder(sda);
sda.Update(dt);
MessageBox.Show("Table updated");
}
}
}
该程序连接到您的 SQL 数据库并从该数据库中检索 SQL 表。使用 button1 更新对数据库的更改。但是,您不能通过此解决方案删除或添加列。
推荐阅读
- javascript - Angular 应用程序基本 url 没有附加静态资源
- flutter - Flutter:如何限制用户在给定时间段内点击按钮的次数?
- openrefine - 如何协调 AGROVOC sparql 端点与 OpenRefine
- react-native - React Native - ImageBackground 未在 TvOS 上显示图像
- python - 如何按每个数据帧的长度拆分/分组数据帧列表
- android - 从 DocumentFile 将文件上传到谷歌驱动器 Rest API V3
- node.js - createWriteStream 的 ('error') 上的开玩笑单元测试
- python - 在 Keras 中使用带有 ImageDataGenerator 的外部注释
- bluetooth-lowenergy - BLE 网关不读取智能手机 BLE 信号
- apache-spark - 合并 Spark SQL - 当源不匹配时