我正在尝试使用npgsql创建我的新asp.net网站。我的数据库如下所示:
[oid, countyname, status]
使用npgsql我已经在gridview中连接并显示数据。我用这段代码完成了这个:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Npgsql;
using System.Data;
using System.Web.Security;
public partial class Secured_pia : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string sQuery = "SELECT countyname, status FROM countydb";
string sConn = "Server=localhost;Port=5432;User Id=user1;Password=pass2;Database=database;";
DataTable DT = new DataTable();
NpgsqlConnection Conn = new NpgsqlConnection(sConn);
Conn.Open();
DataSet DS = new DataSet("DS1");
NpgsqlDataAdapter DA = new NpgsqlDataAdapter();
DA.SelectCommand = new NpgsqlCommand(sQuery, Conn);
DA.Fill(DS, "DS1");
GridView1.DataSource = DS;
GridView1.DataBind();
Conn.Close();
}
}
一切正常。现在我要编辑该表。正如我读得好,我可以使用NpgsqlCommandBuilder来制作插入,更新和删除命令。所以我把代码更改为:
DA.SelectCommand = new NpgsqlCommand(sQuery, Conn);
NpgsqlCommandBuilder UpdateCMD = new NpgsqlCommandBuilder(DA);
DA.Fill(DS, "DS1");
现在,我不知道如何开启编辑。该代码是否足以在gridview中启用编辑?我的aspx代码如下所示:
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:CommandField ShowEditButton="true" />
</Columns>
</asp:GridView>
aspx.cs代码好吗?我是否必须在gridview中添加任何属性? 感谢您的回复,对不起我的简单问题。我是新手。
答案 0 :(得分:0)
我只想在gridview旁边的页面上添加一个按钮。然后 实现您的更新按钮单击事件,如下所示:
public partial class Secured_pia : System.Web.UI.Page
{
String sConn = "Your connection string";
private NpgsqlConnection conn;
private NpgsqlDataAdapter da;
private DataSet ds = new DataSet();
private DataTable dt = new DataTable();
private NpgsqlCommandBuilder cb;
public Secured_pia ()
{
InitializeComponent();
}
//your form load code above goes here but you will have to do
//a global declaration like I did here instead of in the form load event like you have.
//update button implementation.
private void btnUpdate_Click(object sender, EventArgs e)
{
cb = new NpgsqlCommandBuilder(da);
da.Update(dt);
}