简体   繁体   English

ASPxGridView和LinqServerModeDataSource:插入和更新行而不显示网格中的所有列

[英]ASPxGridView and LinqServerModeDataSource: Inserting and updating rows without showing all columns in grid

How do I use a LinqServerModeDataSource to insert or edit rows of the underlying data table when I do not show all fields of that table in the ASPxGridView ? 当我不在ASPxGridView显示该表的所有字段时,如何使用LinqServerModeDataSource插入或编辑基础数据表的行?

Similar questions have been asked, but this is not a duplicate. 已经提出了类似的问题,但这不是重复的。 For example, this one asks about a LinqServerModeDataSource and the accepted answer tells how to use an ordinary SqlDataSource . 例如, 此问题询问LinqServerModeDataSource ,并且接受的答案告诉您如何使用普通的SqlDataSource

I have an ASPxGridView hooked up to a table via a LinqServerModeDataSource . 我有一个ASPxGridView通过挂接到一个表LinqServerModeDataSource But I do not show all columns in the grid. 但是我没有显示网格中的所有列。 For example, there are columns for the date created and some others that the user doesn't need to know about. 例如,有一些用于创建日期的列,其他一些用户不需要知道的列。 I am allowing inline editing in the grid, but in the Inserting or Updating event, the new values passed are just a dictionary of the values displayed in the grid. 我允许在网格中进行内联编辑,但是在“ Inserting或“ Updating事件中,传递的新值只是网格中显示的值的字典。

What about the other values? 其他值呢? I would expect to be able to set any of the values for the underlying data row programmatically in the event handler, regardless of whether they are displayed and thus edited by the user. 我希望能够在事件处理程序中以编程方式设置基础数据行的任何值,而不管它们是否由用户显示并由此进行编辑。 How do I get access to them and set the other values in the events of the LinqServerModeDataSource ? 如何在LinqServerModeDataSource的事件中访问它们并设置其他值? I am having no luck reading the devexpress documentation. 我没有运气来阅读devexpress文档。

I'm guessing that there must be a Linq class that hooks into the table that I can use in those events, similarly to the Selecting event. 我猜想必须有一个Linq类,该类与可以在这些事件中使用的表挂钩,类似于Selecting事件。 But how? 但是如何?

Here's what the Selecting event handler looks like... Is there not some similar interface I can use to access the underlying data in the other events? 这就是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;
}

As much as I hate answering my own question... 我讨厌回答自己的问题...

I can't figure out how to get this control to do what I want. 我不知道如何使该控件执行我想要的操作。 However, a simple workaround is to handle the insert and update on the grid itself. 但是,一个简单的解决方法是处理网格本身的插入和更新。

So, it's working now. 因此,它现在正在工作。 I set the EnableUpdate and EnableInsert properties on the LinqServerModeDataSource to false , and simply handle the grid's RowInserting and RowUpdating events, where I go directly to the database. 我将EnableUpdate上的EnableUpdateEnableInsert属性LinqServerModeDataSourcefalse ,并且只处理网格的RowInsertingRowUpdating事件,在这些事件中直接进入数据库。

For example, my inserting event handler is this: 例如,我的插入事件处理程序是这样的:

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;
}

And my updating event handler is this: 我的更新事件处理程序是这样的:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM