【问题标题】:ASPxGridView and LinqServerModeDataSource: Inserting and updating rows without showing all columns in gridASPxGridView 和 LinqServerModeDataSource:插入和更新行而不显示网格中的所有列
【发布时间】:2015-07-16 21:22:19
【问题描述】:

当我没有在ASPxGridView 中显示该表的所有字段时,如何使用LinqServerModeDataSource 插入或编辑基础数据表的行?

已提出类似问题,但这不是重复的。例如,this one 询问LinqServerModeDataSource,接受的答案告诉如何使用普通的SqlDataSource

我有一个ASPxGridView 通过LinqServerModeDataSource 连接到一张桌子上。但我没有显示网格中的所有列。例如,有创建日期的列和用户不需要知道的其他一些列。我允许在网格中进行内联编辑,但在 InsertingUpdating 事件中,传递的新值只是网格中显示值的字典。

其他值呢?我希望能够在事件处理程序中以编程方式为基础数据行设置任何值,无论它们是否被显示并因此被用户编辑。如何访问它们并在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;
}

【问题讨论】:

    标签: c# devexpress aspxgridview


    【解决方案1】:

    尽管我讨厌回答自己的问题......

    我不知道如何让这个控件做我想做的事。然而,一个简单的解决方法是处理 grid 本身的插入和更新。

    所以,它现在可以工作了。我将LinqServerModeDataSource 上的EnableUpdateEnableInsert 属性设置为false,并简单地处理网格的RowInsertingRowUpdating 事件,我直接进入数据库。

    例如,我的插入事件处理程序是这样的:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多