Wijmo 更优美的jQuery UI部件集:客户端更改C1GridView数据源

发布时间:2012/11/16 00:11 发布者:葡萄城产品团队

返回博客中心

很多时候,我们在使用 GridView 展示数据时,希望最终用户可以编辑数据并且同步到数据源中。这是一项繁琐的工作。我们需要自定义模板列,并且在后台手动获取更新值,最后使用 SQL 语句同步到数据库中。

但是,现在我们有了 C1 Wijmo GridView ,这些繁琐的工作都成为历史。C1GridView 仅仅通过一个属性-AllowClientEditing 便允用户在客户端编辑单元格内容。
需要编辑时,我们可以通过双击单元格使其进入编辑状态即可。完成编辑后,选择其它单元格去保存编辑值。

这篇文章将叙述在不执行任何 PostBack 的情况下,如何轻而易举的更新数据库。

1.定义数据库连接字符串并且绑定到 C1GridView
C1GridView 可以绑定 Oledb 数据源或 SQL 数据源。本文中,我们将使用 Oledb 数据源。请根据下面的代码设置 DataKeyNames 和 C1GridView 相关列。同时,我们需要设定 CallbackSettings 值为 editing ,这样在我们保存时,不会发生 Postback。
参考代码:

 

 

<wijmo:C1GridView ID="C1GridView1" runat="server"
AutogenerateColumns="false" DataKeyNames="CustomerID"
ClientSelectionMode="SingleRow"
AllowClientEditing="true" ShowFilter="true"
OnEndRowUpdated="C1GridView1_EndRowUpdated">
<CallbackSettings Action="Editing, Filtering" />
<Columns>
<wijmo:C1BoundField DataField="CustomerID" HeaderText="CustomerID"
SortExpression="CustomerID">
</wijmo:C1BoundField>
<wijmo:C1BoundField DataField="CompanyName" HeaderText="Company Name"
SortExpression="CompanyName">
</wijmo:C1BoundField>
<wijmo:C1BoundField DataField="ContactName" HeaderText="Contact Name"
SortExpression="ContactName">
</wijmo:C1BoundField>
<wijmo:C1BoundField DataField="City" HeaderText="City"
SortExpression="City">
</wijmo:C1BoundField>
<wijmo:C1BoundField DataField="Country" HeaderText="Country"
SortExpression="Country">
</wijmo:C1BoundField>
</Columns>
</wijmo:C1GridView>
复制代码

下面,我们定义 Oledb 数据库连接字符串。因为需要将更改同步到数据库中,所以我们需要写 SQL 语句去同步数据源。
参考代码:

 

 

public DataTable GetDataTable()
{
DataTable dt = Page.Session["Customers"] as DataTable;
OleDbConnection con = new OleDbConnection("provider=Microsoft.Jet.Oledb.4.0;
Data Source=" + Server.MapPath("~/App_Data/C1NWind.mdb"));
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = new OleDbCommand("SELECT * FROM [Customers] Order By
[CustomerID]", con);
da.UpdateCommand = new OleDbCommand("Update [Customers] set [CompanyName]=?,
[ContactName]=?, [City]=?, [Country]=? where CustomerID = ?", con);
da.UpdateCommand.Parameters.Add("@CompanyName", OleDbType.VarChar, 50,
"CompanyName");
da.UpdateCommand.Parameters.Add("@ContactName", OleDbType.VarChar, 50,
"ContactName");
da.UpdateCommand.Parameters.Add("@City", OleDbType.VarChar, 50, "City");
da.UpdateCommand.Parameters.Add("@Country", OleDbType.VarChar, 50,
"Country");
da.UpdateCommand.Parameters.Add("@CustomerID", OleDbType.VarChar, 50,
"CustomerID");
if (dt == null)
{
dt = new DataTable();
da.Fill(dt);
dt.PrimaryKey = new DataColumn[] { dt.Columns["CustomerID"] };
Page.Session["Customers"] = dt;
}
da.Update(dt);
return dt;
}
复制代码

我们仅需在 RowUpdating 和 EndRowUpdated 事件中更新被编辑的行。在客户端使用 C1 Wijmo GridView 修改数据源。

 
protected void C1GridView1_RowUpdating(object sender,
C1.Web.Wijmo.Controls.C1GridView.C1GridViewUpdateEventArgs e)
{
DataTable customers = GetDataTable();
DataRow row =
customers.Rows.Find(C1GridView1.DataKeys[e.RowIndex].Value);
if (row != null)
{
foreach (DictionaryEntry entry in e.NewValues)
{
row[(string)entry.Key] = entry.Value;
}
}
else
{
throw new RowNotInTableException();
}
Page.Session["Customers"] = customers;
}
复制代码

在 EndRowUpdated 事件中重新绑定 C1GridView 数据源。

 
protected void C1GridView1_EndRowUpdated(object sender,
C1.Web.Wijmo.Controls.C1GridView.C1GridViewEndRowUpdatedEventArgs e)
{
C1GridView1.DataSource = GetDataTable();
C1GridView1.DataBind();
}
复制代码

行可以点击,从而无法保存更改。不要着急!
我们只需要添加 button 去调用 C1GridView 的前台方法 Update即可。

 
<asp:Button ID="btn1" runat="server" Text="Update
C1GridView"OnClientClick="btn_ClientClick(); return false;" />
复制代码

使用下面代码调用 Update() 方法:

 
function btn_ClientClick(sender, args)
{
var grid = $("#C1GridView1");
grid.c1gridview("endEdit");
grid.c1gridview("update");
}
复制代码



Demo 下载:

14460482833746.zip (130.39 K, 下载次数:304)

关于葡萄城

赋能开发者!葡萄城是专业的集开发工具、商业智能解决方案、低代码开发平台于一身的软件和服务提供商,为超过 75% 的全球财富 500 强企业提供服务。葡萄城专注控件软件领域30年,希望通过模块化的开发控件、灵活的低代码应用开发平台等一系列开发工具、解决方案和服务,帮助开发者快速响应复杂多变的业务需求,最大程度地发挥开发者的才智和潜能,让开发者的 IT 人生更从容更美好。

了解详情,请访问葡萄城官网