Hi,
when I try to update database with an async before row update event e.Row cannot be used (IsAddrow, DataChanged and e.Cancel not working)
how can i handle async update without losing link of e.Row param.
If you are trying to use DataBinding (and the WinGrid must be bound to data) and mutlitple threads at the same time, then that's just never going to work. If you are trying to use multiple threads, then you have to be in control of the communication between the threads. But when using DataBinding, the DotNet BindingManager handles the communication between the control (the grid) and the data source, and so there's no way to properly martial the data. So you simply cannot do that safely. The only conceivable way this could possible work is if you bound the grid to some local data source object on the UI Thread. And then added some kind of extra layer that communicates between that local data source and some other thread. That way, you would be completely in control of that communication. But it is by no means trivial and you would have to handle marshalling all communication between the local data source and the background thread which would presumably communicate with some server where there the database resides.
I think problem is about threading, when add ConfigureAwait(false) "Cross-thread operation is not valid: control was accessed from another thread other than the thread from which it was created" error occures.
hi Andrew,
to handle async web api call i convert beforerowupdate event async as shown below. everything is ok until await statement. after that row updated, even if cancelled is set false.
so i need to cancel or let it update as sync process. is i possible with ultra grid?
private async void grdDepartment_BeforeRowUpdate(object sender, CancelableRowEventArgs e) { if (!e.Row.DataChanged) { return; }
if (Document == null || !(Document.Access.Insert || Document.Access.Update)) { MessageBox.Show("Departman Tanımlarında Değişiklik Yapma Yetkiniz Yoktur!", Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); e.Cancel = true;
return; }
e.Row.Cells["Code"].Value = e.Row.Cells["Code"].Value.ToString().Trim(); e.Row.Cells["Name"].Value = e.Row.Cells["Name"].Value.ToString().Trim(); e.Row.Cells["Description"].Value = e.Row.Cells["Description"].Value.ToString().Trim();
try { DepartmentBase department = GridData.ToRow<DepartmentCore>(e.Row).ToBase(Program.Modifier); List<DepartmentBase> departments = new List<DepartmentBase>() { department }; DepartmentResponse response;
//departments.Add(GridData.ToRow<DepartmentCore>(e.Row).ToBase(Program.Modifier));
using (DepartmentAccess DepartmentAccess = new DepartmentAccess(Program.ApiAddress, Program.ApiKey)) { DepartmentAccess.Token = Program.Token;
if (e.Row.IsAddRow) { response = await DepartmentAccess.Add(departments); } else { response = await DepartmentAccess.Update(departments); } }
if (response != null && response.Success != null && response.Success.Count > 0) { ShowTools(false);
if (e.Row.IsAddRow) { e.Row.Cells["DepartmentId"].Value = response.Success.FirstOrDefault(x => x.Data.Code.Trim().ToLower() == e.Row.Cells["Code"].Value.ToString().Trim().ToLower()).Data.DepartmentId; }
Department = GridData.ToRow<DepartmentCore>(e.Row); } else { /f (response.Error.Code == 2) { MessageBox.Show("Veri Tabanına Erişilemiyor!", Text, MessageBoxButtons.OK, MessageBoxIcon.Stop); } else { if (response.Failure.Count > 0) { MessageBox.Show(string.Format("Kayıt İşlemi Başarısız!\nMessage:\t{0}", response.Failure[0].Error.Message), Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } else { MessageBox.Show("Kayıt İşlemi Başarısız!", Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
e.Row.Cells["Code"].Activate(); grdDepartment.PerformAction(UltraGridAction.EnterEditMode);
e.Cancel = true; } } catch (Exception ex) { MessageBox.Show(ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
e.Cancel = true; } }
Hello Abdurrahman,
My team and I have done an initial review of this thread, and I believe we will need a bit more information from your end to really understand what is happening on your end. Can you please provide some further information on the following?
1. Can you please elaborate on what you mean by an “async before row update event” in this case? The UltraGrid owns the BeforeRowUpdate – I am unsure how this would be fired asynchronously?
2. How exactly are you going about doing this asynchronous update such that it fires the BeforeRowUpdate event of the grid?
3. If you have an isolated sample project that demonstrates the behavior you are seeing, this would be most helpful.
Please let me know if you have any other questions or concerns on this matter.
after aysnc call in before row update event, row is updated even e.cancelled is true