ASPxGridView และ LinqServerModeDataSource: การแทรกและอัปเดตแถวโดยไม่แสดงคอลัมน์ทั้งหมดในตาราง

ฉันจะใช้ LinqServerModeDataSource เพื่อแทรกหรือแก้ไขแถวของตารางข้อมูลพื้นฐานได้อย่างไร ในเมื่อฉันไม่ได้แสดงฟิลด์ทั้งหมดของตารางนั้นใน ASPxGridView

มีการถามคำถามที่คล้ายกัน แต่นี่ไม่ใช่คำถามที่ซ้ำกัน ตัวอย่างเช่น อันนี้ ถามเกี่ยวกับ 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;
}

person Jerome Viveiros    schedule 16.07.2015    source แหล่งที่มา


คำตอบ (1)


ถึงขนาดฉันเกลียดการตอบคำถามของตัวเอง...

ฉันไม่สามารถเข้าใจได้ว่าจะให้การควบคุมนี้ทำสิ่งที่ฉันต้องการได้อย่างไร อย่างไรก็ตาม วิธีแก้ไขชั่วคราวง่ายๆ คือจัดการการแทรกและอัปเดตบน ตาราง เอง

ตอนนี้มันใช้งานได้แล้ว ฉันตั้งค่าคุณสมบัติ 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;
}
person Jerome Viveiros    schedule 16.07.2015