我正在尝试实施一个新项目。我想将一个SQL表动态写入例如一个asp:gridview。而且这个网格应该是可编辑的。
我找到了一些解决方案,但是它们都是静态的。但是表必须是动态的,因为我想在网格中包括不同的SQL表。 (SQL表有时会更改,列会被删除或添加新列。因此,静态解决方案不适用。
理想情况下,编辑功能应该像在Excel文档中一样工作。更改数据后,应使用保存按钮将其写回到SQL表。
那有可能吗?至少在过去的四个小时内,我还没有找到解决该问题的方法。
谢谢您的帮助
问候
更新
//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;
}
}
}
答案 0 :(得分:0)
也许是这样的: 在表单上添加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更新对数据库的更改。但是,您无法通过此解决方案删除或添加列。