[英]ASPxGridView and LinqServerModeDataSource: Inserting and updating rows without showing all columns in grid
當我不在ASPxGridView
顯示該表的所有字段時,如何使用LinqServerModeDataSource
插入或編輯基礎數據表的行?
已經提出了類似的問題,但這不是重復的。 例如, 此問題詢問LinqServerModeDataSource
,並且接受的答案告訴您如何使用普通的SqlDataSource
。
我有一個ASPxGridView
通過掛接到一個表LinqServerModeDataSource
。 但是我沒有顯示網格中的所有列。 例如,有一些用於創建日期的列,其他一些用戶不需要知道的列。 我允許在網格中進行內聯編輯,但是在“ Inserting
或“ Updating
事件中,傳遞的新值只是網格中顯示的值的字典。
其他值呢? 我希望能夠在事件處理程序中以編程方式設置基礎數據行的任何值,而不管它們是否由用戶顯示並由此進行編輯。 如何在LinqServerModeDataSource
的事件中訪問它們並設置其他值? 我沒有運氣來閱讀devexpress文檔。
我猜想必須有一個Linq類,該類與可以在這些事件中使用的表掛鈎,類似於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;
}
我討厭回答自己的問題...
我不知道如何使該控件執行我想要的操作。 但是,一個簡單的解決方法是處理網格本身的插入和更新。
因此,它現在正在工作。 我將EnableUpdate
上的EnableUpdate
和EnableInsert
屬性LinqServerModeDataSource
為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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.