ASPxGridView 和 LinqServerModeDataSource:插入和更新行而不显示网格中的所有列
ASPxGridView and LinqServerModeDataSource: Inserting and updating rows without showing all columns in grid
当我没有在 ASPxGridView
中显示 table 的所有字段时,如何使用 LinqServerModeDataSource
插入或编辑基础数据行 table ?
有人问过类似的问题,但这不是重复的。例如,this one 询问 LinqServerModeDataSource
并且接受的答案告诉如何使用普通的 SqlDataSource
.
我有一个 ASPxGridView
通过 LinqServerModeDataSource
连接到 table。但我没有显示网格中的所有列。例如,有创建日期的列和用户不需要知道的其他一些列。我允许在网格中进行内联编辑,但是在 Inserting
或 Updating
事件中,传递的新值只是网格中 displayed 值的字典.
其他值呢?我希望能够在事件处理程序中以编程方式为基础数据行设置任何值,而不管它们是否显示并因此被用户编辑。我如何访问它们并在 LinqServerModeDataSource
的事件中设置其他值?我没法阅读 devexpress 文档。
我猜一定有一个 Linq class 挂接到 table 中,我可以在这些事件中使用它,类似于 Selecting
事件。但是怎么办?
这是 Selecting
事件处理程序的样子...没有一些类似的接口可以用来访问其他事件中的基础数据吗?
protected void dsRecipients_Selecting(object sender, DevExpress.Data.Linq.LinqServerModeDataSourceSelectEventArgs e)
{
SmsRecipientsDataContext context = new SmsRecipientsDataContext();
IQueryable<NotificationParty> val = context.NotificationParties;
int notificationGroupID = Convert.ToInt32(Context.Session["NotificationGroupID"]);
val = val.Where(n => n.NotificationGroupID == notificationGroupID && n.Active);
e.KeyExpression = "ID";
e.QueryableSource = val;
}
虽然我很讨厌回答我自己的问题...
我不知道如何让这个控件做我想做的事。但是,一个简单的解决方法是在 网格 本身上处理插入和更新。
所以,它现在可以工作了。我将 LinqServerModeDataSource
上的 EnableUpdate
和 EnableInsert
属性设置为 false
,并简单地处理网格的 RowInserting
和 RowUpdating
事件,我去了直接到数据库。
例如,我的插入事件处理程序是这样的:
protected void recipientsGrid_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
{
using (SqlConnection connection = new SqlConnection(App_Logic.Wrappers.DatabaseConnectionString()))
{
connection.Open();
using (SqlCommand command = new SqlCommand())
{
command.Connection = connection;
command.Transaction = connection.BeginTransaction();
try
{
command.CommandText = " INSERT INTO NotificationParty(NotificationGroupID, FirstName, LastName, CellNumber, Active, UserCreated, DateCreated) VALUES " +
"(@NotificationGroupID, @FirstName, @LastName, @CellNumber, @Active, @UserCreated, GETDATE())";
command.Parameters.AddWithValue("@NotificationGroupID", Convert.ToInt32(Context.Session["NotificationGroupID"]));
command.Parameters.AddWithValue("@FirstName", e.NewValues["FirstName"]);
command.Parameters.AddWithValue("@LastName", e.NewValues["LastName"]);
command.Parameters.AddWithValue("@CellNumber", e.NewValues["CellNumber"]);
command.Parameters.AddWithValue("@Active", 1);
command.Parameters.AddWithValue("@UserCreated", Session["UID"]);
command.ExecuteNonQuery();
command.Transaction.Commit();
}
catch
{
command.Transaction.Rollback();
}
}
}
recipientsGrid.CancelEdit();
e.Cancel = true;
}
我的更新事件处理程序是这样的:
protected void recipientsGrid_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
{
using (SqlConnection connection = new SqlConnection(App_Logic.Wrappers.DatabaseConnectionString()))
{
connection.Open();
using (SqlCommand command = new SqlCommand())
{
command.Connection = connection;
command.Transaction = connection.BeginTransaction();
try
{
command.CommandText = " UPDATE NotificationParty SET FirstName = @FirstName, LastName = @LastName, CellNumber = @CellNumber, UserModified = @UserModified, DateModified = GETDATE() WHERE ID = @ID";
command.Parameters.AddWithValue("@ID", e.Keys[0]);
command.Parameters.AddWithValue("@FirstName", e.NewValues["FirstName"]);
command.Parameters.AddWithValue("@LastName", e.NewValues["LastName"]);
command.Parameters.AddWithValue("@CellNumber", e.NewValues["CellNumber"]);
command.Parameters.AddWithValue("@UserModified", Session["UID"]);
command.ExecuteNonQuery();
command.Transaction.Commit();
}
catch
{
command.Transaction.Rollback();
}
}
}
recipientsGrid.CancelEdit();
e.Cancel = true;
}
当我没有在 ASPxGridView
中显示 table 的所有字段时,如何使用 LinqServerModeDataSource
插入或编辑基础数据行 table ?
有人问过类似的问题,但这不是重复的。例如,this one 询问 LinqServerModeDataSource
并且接受的答案告诉如何使用普通的 SqlDataSource
.
我有一个 ASPxGridView
通过 LinqServerModeDataSource
连接到 table。但我没有显示网格中的所有列。例如,有创建日期的列和用户不需要知道的其他一些列。我允许在网格中进行内联编辑,但是在 Inserting
或 Updating
事件中,传递的新值只是网格中 displayed 值的字典.
其他值呢?我希望能够在事件处理程序中以编程方式为基础数据行设置任何值,而不管它们是否显示并因此被用户编辑。我如何访问它们并在 LinqServerModeDataSource
的事件中设置其他值?我没法阅读 devexpress 文档。
我猜一定有一个 Linq class 挂接到 table 中,我可以在这些事件中使用它,类似于 Selecting
事件。但是怎么办?
这是 Selecting
事件处理程序的样子...没有一些类似的接口可以用来访问其他事件中的基础数据吗?
protected void dsRecipients_Selecting(object sender, DevExpress.Data.Linq.LinqServerModeDataSourceSelectEventArgs e)
{
SmsRecipientsDataContext context = new SmsRecipientsDataContext();
IQueryable<NotificationParty> val = context.NotificationParties;
int notificationGroupID = Convert.ToInt32(Context.Session["NotificationGroupID"]);
val = val.Where(n => n.NotificationGroupID == notificationGroupID && n.Active);
e.KeyExpression = "ID";
e.QueryableSource = val;
}
虽然我很讨厌回答我自己的问题...
我不知道如何让这个控件做我想做的事。但是,一个简单的解决方法是在 网格 本身上处理插入和更新。
所以,它现在可以工作了。我将 LinqServerModeDataSource
上的 EnableUpdate
和 EnableInsert
属性设置为 false
,并简单地处理网格的 RowInserting
和 RowUpdating
事件,我去了直接到数据库。
例如,我的插入事件处理程序是这样的:
protected void recipientsGrid_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
{
using (SqlConnection connection = new SqlConnection(App_Logic.Wrappers.DatabaseConnectionString()))
{
connection.Open();
using (SqlCommand command = new SqlCommand())
{
command.Connection = connection;
command.Transaction = connection.BeginTransaction();
try
{
command.CommandText = " INSERT INTO NotificationParty(NotificationGroupID, FirstName, LastName, CellNumber, Active, UserCreated, DateCreated) VALUES " +
"(@NotificationGroupID, @FirstName, @LastName, @CellNumber, @Active, @UserCreated, GETDATE())";
command.Parameters.AddWithValue("@NotificationGroupID", Convert.ToInt32(Context.Session["NotificationGroupID"]));
command.Parameters.AddWithValue("@FirstName", e.NewValues["FirstName"]);
command.Parameters.AddWithValue("@LastName", e.NewValues["LastName"]);
command.Parameters.AddWithValue("@CellNumber", e.NewValues["CellNumber"]);
command.Parameters.AddWithValue("@Active", 1);
command.Parameters.AddWithValue("@UserCreated", Session["UID"]);
command.ExecuteNonQuery();
command.Transaction.Commit();
}
catch
{
command.Transaction.Rollback();
}
}
}
recipientsGrid.CancelEdit();
e.Cancel = true;
}
我的更新事件处理程序是这样的:
protected void recipientsGrid_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
{
using (SqlConnection connection = new SqlConnection(App_Logic.Wrappers.DatabaseConnectionString()))
{
connection.Open();
using (SqlCommand command = new SqlCommand())
{
command.Connection = connection;
command.Transaction = connection.BeginTransaction();
try
{
command.CommandText = " UPDATE NotificationParty SET FirstName = @FirstName, LastName = @LastName, CellNumber = @CellNumber, UserModified = @UserModified, DateModified = GETDATE() WHERE ID = @ID";
command.Parameters.AddWithValue("@ID", e.Keys[0]);
command.Parameters.AddWithValue("@FirstName", e.NewValues["FirstName"]);
command.Parameters.AddWithValue("@LastName", e.NewValues["LastName"]);
command.Parameters.AddWithValue("@CellNumber", e.NewValues["CellNumber"]);
command.Parameters.AddWithValue("@UserModified", Session["UID"]);
command.ExecuteNonQuery();
command.Transaction.Commit();
}
catch
{
command.Transaction.Rollback();
}
}
}
recipientsGrid.CancelEdit();
e.Cancel = true;
}