get data in background for datagridview
-
Hi! I'm using a dataGridView (dgv) bound to dataSet through bindingSource. one method (fired by button.click) fills the dataSet using dataAdapter. the dgv displays loaded rows. now what my goal is: when a row is displayed additional data for it is loaded in a background thread, and the row is refreshed (i'm using grid's overriden paint event to display the additional data) the reason for this is that I want to get and display the additional data only if the row is ever displayed, to avoid downloading (from sqldb) data that is not needed.. my current solution is:
public void GetPricesForRow(DataGridViewRow row) { if(backgroundThread != null) while(backgroundThread.ThreadState == System.Threading.ThreadState.Running) Thread.Sleep(3); backgroundThread = new Thread(new ThreadStart(delegate() { try { int colCt = row.DataGridView.Columns.Count; string[] vals = GetAdditionalData(row.Cells[1].Value.ToString().Trim()); row.Cells[colCt - 2].Value = vals[0]; row.Cells[colCt - 1].Value = vals[1]; row.Refresh(); } catch { } })); backgroundThread.Start(); }
row.Refresh(); fires:public void Refresh() { this.DataGridView.InvalidateRow(this.Index); }
well it works.. but pretty slow.. when i set backgroundThread.IsBackground to true the row doesn't refresh (at least not always) anybody knows a better solution?life is study!!!
-
Hi! I'm using a dataGridView (dgv) bound to dataSet through bindingSource. one method (fired by button.click) fills the dataSet using dataAdapter. the dgv displays loaded rows. now what my goal is: when a row is displayed additional data for it is loaded in a background thread, and the row is refreshed (i'm using grid's overriden paint event to display the additional data) the reason for this is that I want to get and display the additional data only if the row is ever displayed, to avoid downloading (from sqldb) data that is not needed.. my current solution is:
public void GetPricesForRow(DataGridViewRow row) { if(backgroundThread != null) while(backgroundThread.ThreadState == System.Threading.ThreadState.Running) Thread.Sleep(3); backgroundThread = new Thread(new ThreadStart(delegate() { try { int colCt = row.DataGridView.Columns.Count; string[] vals = GetAdditionalData(row.Cells[1].Value.ToString().Trim()); row.Cells[colCt - 2].Value = vals[0]; row.Cells[colCt - 1].Value = vals[1]; row.Refresh(); } catch { } })); backgroundThread.Start(); }
row.Refresh(); fires:public void Refresh() { this.DataGridView.InvalidateRow(this.Index); }
well it works.. but pretty slow.. when i set backgroundThread.IsBackground to true the row doesn't refresh (at least not always) anybody knows a better solution?life is study!!!
Hi, Use VirtualMode option of the datagridview. U can find examples in the MSDN HTH. Hayder Marzouk
-
Hi! I'm using a dataGridView (dgv) bound to dataSet through bindingSource. one method (fired by button.click) fills the dataSet using dataAdapter. the dgv displays loaded rows. now what my goal is: when a row is displayed additional data for it is loaded in a background thread, and the row is refreshed (i'm using grid's overriden paint event to display the additional data) the reason for this is that I want to get and display the additional data only if the row is ever displayed, to avoid downloading (from sqldb) data that is not needed.. my current solution is:
public void GetPricesForRow(DataGridViewRow row) { if(backgroundThread != null) while(backgroundThread.ThreadState == System.Threading.ThreadState.Running) Thread.Sleep(3); backgroundThread = new Thread(new ThreadStart(delegate() { try { int colCt = row.DataGridView.Columns.Count; string[] vals = GetAdditionalData(row.Cells[1].Value.ToString().Trim()); row.Cells[colCt - 2].Value = vals[0]; row.Cells[colCt - 1].Value = vals[1]; row.Refresh(); } catch { } })); backgroundThread.Start(); }
row.Refresh(); fires:public void Refresh() { this.DataGridView.InvalidateRow(this.Index); }
well it works.. but pretty slow.. when i set backgroundThread.IsBackground to true the row doesn't refresh (at least not always) anybody knows a better solution?life is study!!!
ok. i got it..
public bool GetPricesForRow(DataGridViewRow row) { ManualResetEvent doneEvent = new ManualResetEvent(false); PricesForRow pfr = new PricesForRow(row.Cells[1].Value.ToString().Trim(), doneEvent); bool x = ThreadPool.QueueUserWorkItem(pfr.ThreadPoolCallback); WaitHandle.WaitAll(new WaitHandle[] { doneEvent }); int colCt = row.DataGridView.Columns.Count; row.Cells[colCt - 2].Value = pfr.OutBold; row.Cells[colCt - 1].Value = pfr.OutNormal; row.Refresh(); return x; }
and an additional class:class PricesForRow { ManualResetEvent _doneEvent; string _param; string _outBold; public string OutBold { get { return _outBold; } set { _outBold = value; } } string _outNormal; public string OutNormal { get { return _outNormal; } set { _outNormal = value; } } public PricesForRow(string param, ManualResetEvent doneEvent) { _param = param; _doneEvent = doneEvent; } public void ThreadPoolCallback(Object threadContext) { try { string[] vals = DataBase.Instance.GetKtmsPrices(_param); _outBold = vals[0]; _outNormal = vals[1]; _doneEvent.Set(); } catch { } } }
this works just fine!! doesn't slowdown the grid or anything..life is study!!!