views:

24

answers:

1

I'm trying to implement database value edition through a DataGridView control but honestly I'm having a hard time trying to accomplish that. I'm basically using LINQ-to-SQL classes and events, the following being the most significant snippets:

var data =  from q in data.FOOBARS
            select new
            {
                ID = q.FOOBAR_ID,
                LOREM = q.FOOBAR_LOREM,
                IPSUM = q.FOOBAR_IPSUM
            };

DataGridView grid = new DataGridView();
grid.DataSource = data;

// grid EVENTS     
private void grid_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    this.grid.CurrentCell.ReadOnly = false;
    this.grid.BeginEdit(true);
}

private void grid_CellLeave(object sender, DataGridViewCellEventArgs e)
{
    if (this.grid.CurrentCell.IsInEditMode)
    {   
        // METHOD CALL            
        this.SetVariableValue(this.grid.CurrentRow.Cells["ID"].Value.ToString(), this.grid.CurrentCell.OwningColumn.Name, this.grid.CurrentCell.FormattedValue.ToString(), this.grid.CurrentCell.EditedFormattedValue.ToString());
    }

    this.grid.CommitEdit(DataGridViewDataErrorContexts.Commit);
    this.grid.EndEdit();
}

// METHOD IMPL
private void SetVariableValue(string id, string type, string current, string edited)
{
    try
    {
        if (current != edited)
        {
            using (FOOBARDataClassesDataContext data = new FOOBARDataClassesDataContext(this.BuildConnection()))
            {
                data.ExecuteCommand("SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED");

                switch (type)
                {
                    case "LOREM":
                        var currentLorem = data.FOOBARS.SingleOrDefault(v => v.FOOBAR_ID == Convert.ToInt32(id)).FOOBAR_LOREM;
                        currentLorem = edited;
                        data.SubmitChanges(ConflictMode.ContinueOnConflict);
                        break;

                    case "IPSUM":
                        var currentIpsum = data.FOOBARS.SingleOrDefault(v => v.FOOBAR_ID == Convert.ToInt32(id)).FOOBAR_IPSUM;
                        currentIpsum = edited;
                        data.SubmitChanges(ConflictMode.ContinueOnConflict);
                        break;

                    default:
                        break;
                }
                data.Refresh(RefreshMode.OverwriteCurrentValues);
            }
        }
    }
    catch (Exception error)
    {
        if (logger.IsErrorEnabled) logger.Error(error.Message);
    }
}

Debugging looks good, objects are actually being updated but for some reason changes are neither being submitted nor updated.

Any help would be certainly appreciated. Thanks much you guys in advance!

A: 

For some reason this does NOT WORK:

var currentLorem = data.FOOBARS.SingleOrDefault(v => v.FOOBAR_ID == Convert.ToInt32(id)).FOOBAR_LOREM;
currentLorem = edited;
data.SubmitChanges(ConflictMode.ContinueOnConflict);

But this DOES (notice the changes in lines 1 and 2, FOOBAR_LOREM attribute):

var currentLorem = data.FOOBARS.SingleOrDefault(v => v.FOOBAR_ID == Convert.ToInt32(id));
currentLorem.FOOBAR_LOREM = edited;
data.SubmitChanges(ConflictMode.ContinueOnConflict);
Nano Taboada